dqbh8054 2019-01-31 13:29
浏览 61

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

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. Like 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).

The table actually contains thousands of records that are not in order. I want to display them as such that the parent followed by its child records and then the next parent and its child records appear and so on. The result need to be sorted by member_id too.

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 have no luck getting the desired results. Doing it programtically using PHP is taking too long since I have to iterate each record many times. Any help will be appreciated. Thanks in advanced.

Edit: 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  |
+-----------+--------------+--------------+----------+
  • 写回答

1条回答 默认 最新

  • dpbfb7119 2019-02-02 00:32
    关注

    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

    评论

报告相同问题?

悬赏问题

  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画