无限级分类(或菜单)设计

idtitlepathlevelpaths
1衣物000,1
2上衣0,110,1,2
7衬衫0,1,220,1,2,7
3裤子0,110,1,3
4西裤0,1,320,1,3,4
5长西裤0,1,3,430,1,3,4,5
6短西裤0,1,3,430,1,3,4,6

其中,path表示父级id列表。例如西裤的0,1,3就表示衣物 > 裤子
实现面包屑时只要取出path字段的值,然后用,分割,得到id列表
再用where id in (1,3)即可

需要显示所有分类并表示层级时,使用下面的SQL:

SELECT *,concat(path,',',id) AS paths FROM menu ORDER BY paths

懒加载

SELECT
dept.id,
dept.path,
dept.paths,
dept.title AS title,
dept.id AS "value",
dept.id AS "key",
(
SELECT
CASE WHEN count(1) > 0 THEN 1 ELSE 0 END
FROM
menu 
WHERE
parent_id = dept.id and is_deleted = 0
) AS "has_children"
FROM
menu  dept
WHERE
dept.path = 1 AND dept.is_deleted = 0
ORDER BY dept.sort
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值