MySQL自连接获取有序的父子记录

时间:2019-01-31 13:29:40

标签: php mysql hierarchical-data self-join

我有一个具有以下结构的表:

+-----------+--------------+-----------+----------+
| member_id | household_id | firstName | lastName |
+-----------+--------------+-----------+----------+
| 14122     | 0            | Cynthia   | Bookout  |
+-----------+--------------+-----------+----------+
| 14123     | 0            | Kim       | Caves    |
+-----------+--------------+-----------+----------+
| 14124     | 14122        | Marvin    | Bookout  |
+-----------+--------------+-----------+----------+
| 14125     | 13974        | Stacey    | Webb     |
+-----------+--------------+-----------+----------+
| 14126     | 13974        | Nathan    | Webb     |
+-----------+--------------+-----------+----------+
| 14127     | 13974        | Adam      | Webb     |
+-----------+--------------+-----------+----------+
| 14128     | 13974        | Thomas    | Webb     |
+-----------+--------------+-----------+----------+
| 14129     | 0            | Missy     | Hammock  |
+-----------+--------------+-----------+----------+
| 14130     | 0            | Stephanie | Lewis    |
+-----------+--------------+-----------+----------+
| 14131     | 0            | Kelly     | Hutto    |
+-----------+--------------+-----------+----------+
| 14132     | 14130        | James     | Lewis    |
+-----------+--------------+-----------+----------+
| 14133     | 0            | Cindy     | Barnwell |
+-----------+--------------+-----------+----------+
| 14134     | 13828        | NATALIE   | MCMILLAN |
+-----------+--------------+-----------+----------+
| 14135     | 13828        | Steven    | Adams    |
+-----------+--------------+-----------+----------+
| 14136     | 0            | Katherine | Gaskins  |
+-----------+--------------+-----------+----------+

member_idhousehold_id对于某些记录具有父子关系。例如,具有member_id 14124的记录是member_id 14122的子代(即,其family_id是其父代的member_id)。

该表实际上包含数千个不按顺序的记录。我想将其显示为使得母随后其子记录,然后下一个父及其子记录显示等。结果需要由member_id过于进行排序。

我已经尝试过了:

SELECT * FROM members WHERE household_id IN (SELECT member_id FROM members WHERE household_id = 0) OR household_id = 0
ORDER BY member_id

但是没有运气能得到理想的结果。使用PHP以编程方式进行此操作花费的时间太长,因为我必须多次遍历每条记录。 任何帮助将不胜感激。谢谢你。

编辑:这是我的期望的结果:

+-----------+--------------+--------------+----------+
| member_id | household_id | firstName    | lastName |
+-----------+--------------+--------------+----------+
| 14122     | 0            | Cynthia      | Bookout  |
+-----------+--------------+--------------+----------+
| 14124     | 14122        | Marvin Keith | Bookout  |
+-----------+--------------+--------------+----------+
| 14123     | 0            | Kim          | Caves    |
+-----------+--------------+--------------+----------+
| 14125     | 13974        | Stacey       | Webb     |
+-----------+--------------+--------------+----------+
| 14126     | 13974        | Nathan       | Webb     |
+-----------+--------------+--------------+----------+
| 14127     | 13974        | Adam         | Webb     |
+-----------+--------------+--------------+----------+
| 14128     | 13974        | Thomas       | Webb     |
+-----------+--------------+--------------+----------+
| 14129     | 0            | Missy        | Hammock  |
+-----------+--------------+--------------+----------+
| 14130     | 0            | Stephanie    | Lewis    |
+-----------+--------------+--------------+----------+
| 14132     | 14130        | James        | Lewis    |
+-----------+--------------+--------------+----------+
| 14131     | 0            | Kelly        | Hutto    |
+-----------+--------------+--------------+----------+
| 14133     | 0            | Cindy        | Barnwell |
+-----------+--------------+--------------+----------+
| 14134     | 13828        | NATALIE      | MCMILLAN |
+-----------+--------------+--------------+----------+
| 14135     | 13828        | Steven       | Adams    |
+-----------+--------------+--------------+----------+
| 14136     | 0            | Katherine    | Gaskins  |
+-----------+--------------+--------------+----------+

1 个答案:

答案 0 :(得分:0)

尝试以下查询:

SELECT a.member_id as parent_id,
       a.firstName as parent_firstName,
       a.lastName as parent_lastName,
       b.member_id as child_id,
       b.firstName as child_firstName,
       b.lastName as child_lastName
FROM members a inner join members b on a.member_id = b.household_id
ORDER BY a.member_id

query_output