Mysql:获取表中所有链接的行
表结构
我有一个有点像这样的
id | title | next
1 | test | 5
2 | test | 0
3 | test | 0
4 | test | 0
5 | test | 3
现在,正如你所看到的,1指向下一项5,5指向下一项3,3表示
我需要一个查询,从中我可以在一列中连续获取1,5,3他们的标题也
喜欢
result | title
--------------
1 | test
5 | test
3 | test
--------
请帮忙。我什至不知道如何开始这样的查询。
I have a table structure
kinda like this
id | title | next
1 | test | 5
2 | test | 0
3 | test | 0
4 | test | 0
5 | test | 3
Now, as you see 1 points to next item 5 and 5 points to next item 3 and 3 denotes the end
I need a query, from which I can get 1, 5, 3 serially in one column and their title also
like
result | title
--------------
1 | test
5 | test
3 | test
--------
please help. I dont even know how to get started at such query.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您需要的是树查询 - 检查
是否可以在单个查询中查询 MySQL 中的树形结构表,达到任意深度?
What you need is a tree query - check
Is it possible to query a tree structure table in MySQL in a single query, to any depth?
您在这里要做的是将表本身连接起来。
您需要为表的两个副本提供自己的别名(此处:父级和子级),否则您将遇到唯一性问题。
What you want to do here is join the table on itself.
You need to give both copies of the table their own alias (here: parent and child) because otherwise you will run into uniqueness troubles.
一种方法是创建一个重复简单查询的循环。
我可以在这里发布一个例子。您使用 PHP 吗?
One way would be to create a loop that repeats a simple query..
I could post an example here. Are you using PHP?