使用php运行多级mysqli查询的最有效方法

I have a table:

enter image description here

I have a list of id's that I have selected (highlighted)

Using php and mysqli, how can I output data to the page (sorted by decending 'value', followed by the respective 'value's ascending 'name') in the most query-efficient way.

Example:

  • 9
    • bbbbb
  • 5
    • ddddd
  • 4
    • 11111
    • hhhhh
  • 3
    • ccccc
    • fffff

Try this ($myIDArray is from a previous query):

$myIDArray = [2,5,6,7,9,11]; //From previous query
$sql = "SELECT value, GROUP_CONCAT(name ORDER BY name DESC SEPARATOR ',')
    FROM your_table 
    WHERE value IN (".implode(',', $myIDArray)."
    GROUP BY value
    ORDER BY value DESC";

After you get the result set, go through each row of the result and do something like this:

echo "<ul>";
foreach($rows as $row)
{
    echo "<li>$row->value <ul><li>";
    echo implode('</li><li>', explode(",", $row->name));
    echo "</li></ul></li>";
}
echo "</ul>";