mysql,从表中选择全部
我有这张桌子。
我想选择 cat id = 4 且每隔一行其 cat 父 id 等于。 例如,如果我想要 cat id 4 中的所有类别,则应该得到 cat id 4(本身)、2 和 1
+-------------------------------+
| id | catname | catparentid |
+-------------------------------+
|1 | home | 0 |
|2 | products | 1 |
|3 | men | 2 |
|4 | women | 2 |
|5 | shirts | 3 |
|6 | outdoor | 0 |
+-------------------------------+
我尝试过:
SELECT * FROM categories c
where c.id = 4
c.catparentid IN (SELECT id FROM categories)
但它会显示所有类别。我需要成立工会吗?
I have this table.
I want to select cat id = 4 and every other row its cat parent id equals to.
e.g. if I want all categories from cat id 4, it should result to cat ids 4 (itself), 2 and 1
+-------------------------------+
| id | catname | catparentid |
+-------------------------------+
|1 | home | 0 |
|2 | products | 1 |
|3 | men | 2 |
|4 | women | 2 |
|5 | shirts | 3 |
|6 | outdoor | 0 |
+-------------------------------+
I tried:
SELECT * FROM categories c
where c.id = 4
c.catparentid IN (SELECT id FROM categories)
But it brings up all. Do I need to do a union?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
联合在这里没有帮助,因为你不知道需要多少个联合(树的深度)。
较新的SQL标准支持传递闭包的计算,我认为使用WITH,但我不知道当前的SQL服务器有多少支持。
Union won't help here because you don't know how many unions you need (depth of the tree).
Newer SQL standards support the calculation of the transitive closure, I think using WITH, but I don't know how much support is available in current SQL servers.