select A.title,(case when A.lft is null then C.num when A.lft is not null and A.rgt is not null then select count(*) from B where category_id=A.lft or category_id=A.rgt) from A left join ( select category_id,count(*) as num from B group by category_id )C on A.id=C.category_id
id title lft rgt 1 类别一 2 小类别a 3 小类别b 4 类别二 5 小类别c 6 小类别d你是如何判断哪一行是大类,哪一行是小类的? 另外如休知道某一个是小类并且属于哪个大类?
id title lft rgt
1 类别一 2 3
2 小类别a
3 小类别b
4 类别二 5 6
5 小类别c
6 小类别d
from A left join (
select category_id,count(*) as num from B group by category_id
)C on A.id=C.category_id
1 类别一
2 小类别a
3 小类别b
4 类别二
5 小类别c
6 小类别d你是如何判断哪一行是大类,哪一行是小类的? 另外如休知道某一个是小类并且属于哪个大类?
他这个zone表是个左右值的小变种,他加了个root的字段,排序是按照root,lft来排的。这个跟查询应该关系不大,加多个where root=x就行。
zone表如有需要,可以添加parent_id字段,再不行加个path用来保存父到子的路径也行(,1,2,3,x...)第二个表是这样的: agent统计父地区及子地区的agent有多少个。
比如深圳(zone_id=2)的店有8个,广东的店(zone=1)有22个。