简体   繁体   English

Mysql:获取表中的所有链接行

[英]Mysql: Get all the linked rows in a table

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 现在,正如你看到1点到下一个项目5和5点到下一个项目3和3表示结束

I need a query, from which I can get 1, 5, 3 serially in one column and their title also 我需要一个查询,从中我可以在一列中连续获得1,5,3,它们的标题也是如此

like 喜欢

result | title
--------------
1      |  test
5      |  test
3      |  test
--------

please help. 请帮忙。 I dont even know how to get started at such query. 我甚至不知道如何开始这样的查询。

What you want to do here is join the table on itself. 你想要做的就是加入桌子。

SELECT * FROM `table` AS `child` 
JOIN `table` AS `parent` 
ON `parent`.`next` = `child`.`id`

You need to give both copies of the table their own alias (here: parent and child) because otherwise you will run into uniqueness troubles. 您需要为表的两个副本提供他们自己的别名(这里:父和子),否则您将遇到唯一性问题。

One way would be to create a loop that repeats a simple query.. I could post an example here. 一种方法是创建一个重复简单查询的循环..我可以在这里发布一个例子。 Are you using PHP? 你在用PHP吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM