获取一个 SQL 查询中的所有父行
我有一个简单的 MySQL 表,其中包含类别列表,级别由parent_id 确定:
id name parent_id
---------------------------
1 Home 0
2 About 1
3 Contact 1
4 Legal 2
5 Privacy 4
6 Products 1
7 Support 1
我正在尝试创建面包屑路径。所以我有孩子的“id”,我想获得所有可用的父母(迭代链直到我们到达 0“Home”)。可以有任何数量或子行,深度不受限制。
目前我正在为每个父级使用 SQL 调用,这很混乱。 SQL 有没有一种方法可以在一个查询中完成这一切?
I have a simple MySQL table thats contains a list of categories, level is determined by parent_id:
id name parent_id
---------------------------
1 Home 0
2 About 1
3 Contact 1
4 Legal 2
5 Privacy 4
6 Products 1
7 Support 1
I'm attempting to make a breadcrumb trail. So i have the 'id' of the child, I want to get all available parents (iterating up the chain until we reach 0 "Home"). There could be any number or child rows going to an unlimited depth.
Currently I am using an SQL call for each parent, this is messy. Is there a way in SQL to do this all on one query?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
改编自此处< /a>:
@r := 5
行是当前页的页码。结果如下:Adapted from here:
The line
@r := 5
is the page number for the current page. The result is as follows:接受的答案具有递归检索子用户的所有父用户的最佳解决方案。我已根据我的需要对此进行了修改。
对于 MySQL 5.5、5.6 和5.7
注意:@r := 9。其中9是子用户的id。
请参阅此处的小提琴
上面的查询在 MySQL 8 中已弃用。所以这里是MySQL 8.0 的查询
注意:id = 9。其中 9 是子用户的 id。
在此处查看小提琴
The Accepted answer has the best solution to retrieve all parent users of child user recursively. I have modified this as per my need.
For MySQL 5.5, 5.6 & 5.7
Note : @r := 9. Where 9 is the child user's id.
See fiddle here
The above query is deprecated in MySQL 8. So here is the query for MySQL 8.0
Note : id = 9. Where 9 is the child user's id.
See fiddle here
马克·拜尔斯的回答非常棒!
也许有点晚了,但如果您还想在 id =parent_id 时防止无限循环(即,当数据已损坏时),您可以像这样扩展答案:
Awesome answer by Mark Byers!
Maybe a bit late to the party, but if you also want to prevent an infinite loop when id = parent_id (i.e. somehow when data has been corrupted), you can expand the answer like this:
除了上述解决方案之外:
包括家长在内的作者可以访问该帖子。
我想检查作者是否有权访问该帖子。
解决方案:
给出帖子作者的id并返回其所有作者和作者的父母
@r := 2 =>为@r变量赋值。
In addition to the above solutions:
an author including parents can have an access to the post.
I want to check whether author has an access to the post.
Solution:
give the post author's id and return all its authors and author's parents
@r := 2 => assigning value to @r variable.
我使用之前的答案作为示例,以使其更具可读性。
执行结果如下所示:
(只是为了快速)
要亲自检查,您需要将问题中的值输入数据库
test
、表organizations
I used the previous answers as examples to make smth more readable.
The result of execution looks like that:
(just for quick)
to check it yourself you need to enter values from the question into database
test
, tableorganizations
我认为,使用一个查询没有简单的方法可以做到这一点。
我建议看看 嵌套集,看起来以满足您的需求。
I think, there's no easy way to do that, using one query.
I would recommend to take a look at Nested Sets, that seems to fit your needs.
如果您有 slug 而不是 id,则只需运行子查询即可查找子类别的 id。
表 - 类别
|编号 |父 ID |蛞蝓 |
|------------------------------------|
| 1 0 | u1 |
| 2 1 | u2 |
| 3 2 | u3 |
| 4 0 | u4 |
If You have slug instead of id then simply run sub-query to find id of child category.
Table - categories
| id | parentId | slug |
|-------------------------|
| 1 | 0 | u1 |
| 2 | 1 | u2 |
| 3 | 2 | u3 |
| 4 | 0 | u4 |
当我为自己的分层表制定解决方案时,我研究了 WP 多级类别模型。根据此处提供的出色答案,我进行了此查询以获取 Wordpress 数据库中的父类别。我不是这方面的专家,但这对我有用,并且可能对寻找此类答案的人有所帮助。
While I was working on a solution for my own hierarchical table I looked at the WP multilevel category model. Based on the excellent answer provided here I made this query to get the parent categories in a Wordpress database. I'm not expert in the matter but this worked on my end and might be of help to someone looking for such answer.
据我所知没有。
这篇 Sitepoint 文章可能会对您有所帮助。
您可以使用一个查询检索所有元素,将其存储在一个数组中,然后进行迭代,
如此处所述和此处
AFAIK no.
This Sitepoint article may help you.
You could retrieve all the elements with one query, store it in an array and then iterate,
as explained here and here