Mysql: get all related rows in a table

I have a table structure

like this

id | title | next 1 | test | 5 2 | test | 0 3 | test | 0 4 | test | 0 5 | test | 3 

Now that you see 1 point to the next point 5 and 5 points to the next point 3 and 3 marks the end

I need a query from which I can get 1, 5, 3 in turn in one column and their name also

as

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

please, help. I don’t even know how to get started with such a request.

+4
source share
3 answers

What you want to do here, join the table.

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

You need to provide both copies of the table with your own alias (here: parent and child), because otherwise you will encounter unique problems.

+1
source

One way is to create a loop that repeats a simple query. I could give an example here. Do you use PHP?

0
source

Source: https://habr.com/ru/post/1346730/


All Articles