查询以获取子类别并了解子类别是否有自己的子类别
我有一个 cat
表,其中包含以下列:
cat_id | name | parent_id
1 cat1 1
2 subcat1 1
3 subcat1-subcat 2
该表有数千个类别,但这是一般结构。
当用户选择顶级类别时,我有一个查询来获取其子类别,如下所示:
SELECT * FROM cat WHERE parent = $id
我的问题是我需要知道这些子类别是否有自己的子类别。
我可以对结果进行循环并对返回的每个类别进行查询,但我希望有一种解决方案,我可以只使用一个查询,也许它需要一个子查询?
感谢您的帮助。
I have a cat
table that has the following columns:
cat_id | name | parent_id
1 cat1 1
2 subcat1 1
3 subcat1-subcat 2
This table has thousands of categories but that is the general structure.
When a user selects a top level category I have a query to get its children like this:
SELECT * FROM cat WHERE parent = $id
My problem is that I need to know if these children categories have children of their own.
I could do a loop on the results and do a query for each category returned, but I am hoping that there is a solution where I can use just one query, maybe it will require a sub query?
Thanks for the help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用子查询来检查是否有子节点:
You could use a subquery to check if there are child nodes:
如果存在子类别,则计数列将不为零。
The count column will be non-zero if there are sub-categories.