如何按组菜单排序数据?
MySQL的
SELECT pc.pc_id, pc.pc_icon, pc.pc_parent, pcd.pcd_name, pcd.pcd_id
FROM product_category pc
LEFT JOIN product_category_detail pcd ON pcd.pc_id=pc.pc_id
ORDER BY pc.pc_group, pc.pc_sort, pc.pc_id
我想显示内容菜单
例如
测试6
-subtest6
--subtest6-2
--- subtest6-2-1
测试7
我如何查询呢?
谢谢
最佳答案
如果您想拥有无限的子菜单,使用PHP循环可能会更容易。
产生2级菜单仍然很容易,例如
SELECT a.id,
FROM
SELECT pc.pc_id, pc.pc_icon, pc.pc_parent, pcd.pcd_id,
concat(if (pc.pc_parent = 0, '', '- '), pcd.pcd_name) AS pcd_name
FROM product_category AS pc
LEFT JOIN product_category_detail AS pcd ON pcd.pc_id = pc.pc_id
按pc.pc_group,pc.pc_sort,pc.pc_id排序
或者,您可以重新设计类别表以具有简单且无限制的子菜单树:
pc_id
11
1122 -- kid of 11
112233 -- kid of 1122
55
66
6677 -- kid of 66