我有User表,其中包含以下列id,ancestor_id,parent_id,level。我想根据按级别排序的parent_id列出用户。
select id, name, ancestor_id, parent_id, level from users;
+------+---------+-------------+-----------+-------+
| id | name | ancestor_id | parent_id | level |
+------+---------+-------------+-----------+-------+
| 1 | John | NULL | 1 | 0 |
| 2 | Mark | 1 | 1 | 0 |
| 3 | Stephen | 1 | 1 | 1 |
| 4 | Richard | 1 | 2 | 0 |
| 5 | Michael | 1 | 2 | 1 |
| 6 | Paul | 1 | 2 | 2 |
| 7 | Emily | 1 | 6 | 0 |
| 8 | Mathew | 1 | 6 | 1 |
+------+---------+-------------+-----------+-------+
输出:
John
Mark (first level)
Richard (2nd level)
Michael (2nd level)
Paul (2nd level)
Emily (3rd level)
Mathew (3rd level)
Stephen(first level)
如何编写mysql以列出按其适当父级的级别排序的用户?