如何通过mysql查询选择类别和子类别以及多子类别? 像这样:
Main category 1
category 1
category 2
category 3
category 4
category 5
Main category 2
category 6
category 7
category 8
category 9
category 10
我尝试了这段代码,但它不起作用
$cats = mysqli_query($con, "select id,name from categories order by id ASC");
while($row = mysqli_fetch_array($cats)){
$a[$row['name']] = $row;
$has_sub = mysqli_query($con, "select id,name from categories where parent_id=".$row['id']);
if(mysqli_num_rows($has_sub)>0)
$a[$row['name']]['sub'] = mysqli_fetch_array($has_sub);
}
答案 0 :(得分:0)
现在回答有点晚了,但对于仍在搜索此内容的任何人:
你可以在 MySQL 中使用带有 WITH
关键字的递归查询来做这样的事情