gpt4 book ai didi

php - 帮助加入和分组

转载 作者:行者123 更新时间:2023-11-29 07:11:41 27 4
gpt4 key购买 nike

必须有一种方法使它只使用一个查询。我假设使用 join 和 group by

这是我当前的代码:

$sql = mysql_query("select * from `databases` where `id` in (select `id_database` from `categories`)");

while ($row = mysql_fetch_assoc($sql))
{
echo $row['name'] . '<br />';

$sql_c = mysql_query("select * from `categories` where `id_database`='" . $row['id'] . "'");

while ($row_c = mysql_fetch_assoc($sql_c))
{
echo $row_c['title'] . '<br />';
}
}

最佳答案

有...像这样的东西应该可以解决问题。

$sql = mysql_query("select * from databases db JOIN categories cat on (db.id=cat.id_database) ORDER BY db.name");

$last_name = null;
while ($row = mysql_fetch_assoc($sql))
{
if( $row['db.name'] !== $previous_name ) {
echo $row['db.name'] . '<br />';
$previous_name = $row['db.name'];
}
echo $row['cat.title'] . '<br />';
}

关于php - 帮助加入和分组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4104196/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com