简体   繁体   English

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

[英]MySQL self join to get ordered parent-child records

I have a table with the following structure:我有一个具有以下结构的表:

+-----------+--------------+-----------+----------+
| 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_id and household_id have a parent-child relationship for certain records. member_idhousehold_id对某些记录的父子关系。 For example, record with member_id 14124 is the child of member_id 14122 (ie, its household_id is the member_id of its parent).例如,记录member_id 14124是孩子member_id 14122(即其household_idmember_id其父的)。

The table actually contains thousands of records that are not in order.该表实际上包含数以千计的无序记录。 I want to display them such that the parent is followed by its child records, and then the next parent and its child records appear, and so on.我想显示它们,以便父项后跟其子记录,然后出现下一个父项及其子记录,依此类推。 The result needs to be sorted by member_id , too.结果也需要按member_id排序。

I have tried this:我试过这个:

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

But I do not get the desired results.但我没有得到想要的结果。 Doing it programatically using PHP is taking too long since I have to iterate each record many times.使用 PHP 以编程方式执行此操作花费的时间太长,因为我必须多次迭代每条记录。

This is my desired result:这是我想要的结果:

+-----------+--------------+--------------+----------+
| 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  |
+-----------+--------------+--------------+----------+

Try the following query:尝试以下查询:

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查询输出

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM