我有以下'departments'
表:
+-------+-------------+-------------------+
| rowid | department | parent_department |
+-------+-------------+-------------------+
| 10 | Main Office | NULL |
+-------+-------------+-------------------+
| 11 | Back Office | 10 |
+-------+-------------+-------------------+
| 12 | Commercial | NULL |
+-------+-------------+-------------------+
| 13 | Outdoor | NULL |
+-------+-------------+-------------------+
| 14 | Beach | 13 |
+-------+-------------+-------------------+
| 15 | Gardening | 13 |
+-------+-------------+-------------------+
| 16 | Accounting | 10 |
+-------+-------------+-------------------+
我想根据rowid和parent_department这样订购它:
+-------+-------------+-------------------+
| rowid | department | parent_department |
+-------+-------------+-------------------+
| 10 | Main Office | NULL |
+-------+-------------+-------------------+
| 11 | Back Office | 10 |
+-------+-------------+-------------------+
| 16 | Accounting | 10 |
+-------+-------------+-------------------+
| 13 | Outdoor | NULL |
+-------+-------------+-------------------+
| 14 | Beach | 13 |
+-------+-------------+-------------------+
| 15 | Gardening | 13 |
+-------+-------------+-------------------+
| 12 | Commercial | NULL |
+-------+-------------+-------------------+
因此,对于每个 rowid ,查找是否存在'parents_departments'
并按以下顺序显示。
请注意,我没有设计当前的表格,遗憾的是我没有更改权限
答案 0 :(得分:2)
试试这个:
SELECT rowid, department, parent_department
FROM departments
ORDER BY IFNULL(parent_department, rowid), rowid;