I have a MySQL table looking like this:
> describe books;
+---------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------------+--------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| title | varchar(255) | YES | | NULL | |
| after | int(11) | YES | | NULL | |
+---------------+--------------+------+-----+---------+----------------+
Now, I want to select all rows from the table, but as sorted linked list. after
is an id
of row it should be after in query result. Of course I could do that after query in my script using this table, but I'd like to know if there is a way to get the rows sorted in such way.
Example of data in table:
(1, "title #1", 0)
(2, "title #2", 3)
(3, "title #3", 1)
(4, "title #4", 2)
Expected result:
(1, "title #1", 0)
(3, "title #3", 1)
(2, "title #2", 3)
(4, "title #4", 2)