doumeikuan6834 2016-11-19 21:52
浏览 25
已采纳

使用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
  • 写回答

1条回答 默认 最新

  • douhao2856 2016-11-19 22:25
    关注

    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>";
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?