gpt4 book ai didi

mysql 处理树

转载 作者:行者123 更新时间:2023-11-29 13:39:25 24 4
gpt4 key购买 nike

我得到了这张表:

CREATE TABLE 'category' (
'id' INT(11) NOT NULL AUTO_INCREMENT,
'parent_category_id' INT(11) NULL DEFAULT NULL,
'name' VARCHAR(100) NOT NULL,
PRIMARY KEY ('id'),
INDEX 'parent_category_id' ('parent_category_id'),
CONSTRAINT 'category_ibfk_1' FOREIGN KEY ('parent_category_id') REFERENCES 'category' ('id')
) COLLATE='utf8_general_ci' ENGINE=InnoDB;

如何选择子类别少于 3 个的类别(无深度),以及如何选择没有子元素的类别。谢谢!

最佳答案

少于三个:

select parent.*
from category parent left outer join
category child
on parent.id = child.parent_category_id
group by parent.id
having count(child.id) < 3

对于没有类别:

select parent.*
from category parent left outer join
category child
on parent.id = child.parent_category_id
where child.id is null

关于mysql 处理树,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18293783/

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