简体   繁体   English

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

[英]Most efficient way to run multi-level mysqli query with php

I have a table: 我有一张桌子:

在此处输入图片说明

I have a list of id's that I have selected (highlighted) 我有一个已选择的ID列表(突出显示)

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. 使用php和mysqli,我如何以查询效率最高的方式将数据输出到页面(按降序排列的“值”,然后是各个“值的升序”名称)。

Example: 例:

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

Try this ($myIDArray is from a previous query): 试试这个($ myIDArray来自上一个查询):

$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>";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM