找出没有孩子的ids订购父母和祖父母

时间:2016-03-21 17:45:23

标签: php mysql

表名:level

+-----+-------------------+----------+
| id  | name              | parentid |
+-----+-------------------+----------+
| 1   |  AIR CONDITIONER  |  0       |
| 2   |  TV               |  0       |
| 3   |  SPLIT TYPE       |  1       |
| 4   |  WINDOWS TYPE     |  1       |
| 5   |  LCD              |  2       |
| 6   |  PLASMA           |  2       |
| 7   |  MUSIC PLAYER     |  0       |
| 8   |  AC 0.75 TON      |  3       |
| 9   |  AC 1 TON         |  3       |
| 10  |  WT 0.75 TON      |  4       |
| 11  |  WT 1 TON         |  4       |
| 12  |  LCD 32" UP       |  5       |
| 13  |  LCD 4" UP        |  5       |
| 14  |  PHOTO ALBUMS     |  0       |
| 15  |  STANDUP TYPE     |  1       |
| 16  |  CD PLAYERS       |  7       |
| 17  |  MP3 PLAYERS      |  7       |
| 18  |  CDS              |  0       |
| 19  |  BOOKS            |  0       |
| 20  |  PORTABLE TV      |  2       |
+-----+-------------------+----------+

首先,我厌倦了通过以下查询找出所有没有孩子的ID:

SELECT
        t1.id,
        t1.name AS name,
        t2.id AS parentid,
        t2.name AS parent_name,
        t3.id AS grandparentid,
        t3.name AS grandparent_name
FROM level t1
        LEFT JOIN level t2 ON t1.parentid=t2.id
        LEFT JOIN level t3 ON t2.parentid=t3.id
        WHERE t1.id NOT IN (SELECT parentid from level)

获得以下结果

+-----+--------------+----------------+-------------------+
| id  |    name      | parent_name    | grandparent_name  |
+-----+--------------+----------------+-------------------+
|  6  | PLASMA       | TV             | NULL              |
|  8  | AC 0.75 TON  | SPLIT TYPE     | AIR CONDITIONER   |
|  9  | AC 1 TON     | SPLIT TYPE     | AIR CONDITIONER   |
| 10  | WT 0.75 TON  | WINDOWS TYPE   | AIR CONDITIONER   |
| 11  | AC 1 TON     | WINDOWS TYPE   | AIR CONDITIONER   |
| 12  | LCD 32" UP   | LCD            | TV                |
| 13  | LCD 42" UP   | LCD            | TV                |
| 14  | PHOTO ALBUM  | NULL           | NULL              |
| 15  | STANDUP TYPE | AIR CONDITIONER| NULL              |
| 16  | CD PLAYERS   | MUSIC PLAYER   | NULL              |
| 17  | MP3 PLAYERS  | MUSIC PLAYER   | NULL              |
| 18  | CDS          | NULL           | NULL              |
| 19  | BOOKS        | NULL           | NULL              |
| 19  | BOOKS        | NULL           | NULL              |
+-----+--------------+----------------+-------------------+

这只会获得没有子记录的ID 但是我需要创建一个选项菜单,它更加用户友好:

我尝试了ORDER BY t2.parentid DESC,t1.prentid DESC但没有得到我想要的方式

示例(您可以忽略此示例。只是为了了解我需要实现的目标):

<option>Air Conditioner >> STANDUP TYPE</option>
<option>Air Conditioner >> SPLIT TYPE >> AC 0.75 TON</option>
<option>Air Conditioner >> SPLIT TYPE >> AC 1 TON</option>
<option>Air Conditioner >> WINDOWS TYPE >> AC 0.75 TON</option>
<option>Air Conditioner >> WINDOWS TYPE >> AC 1 TON</option>
<option>BOOKS</option>
<option>CDS</option>
<option>MUSIC PLAYERS >> MP3 PLAYERS</option>
<option>MUSIC PLAYERS >> CDS PLAYERS</option>
<option>PHOTO ALBUM</option>
<option>TV >> LCD >> LCD 32" UP</option>
<option>TV >> LCD >> LCD 42" UP</option>
<option>TV >> PLASMA</option>
<option>TV >> PORTABLE TV</option>

所以我需要找出一个不同的查询:

预期结果

+-----+-------------------+------------------+--------------+
| id  | grandparent_name  | parent_name      |    name      |
+-----+-------------------+------------------+--------------+
| 15  | NULL              | AIR CONDITIONER  | STANDUP TYPE |
|  8  | AIR CONDITIONER   | SPLIT TYPE       | AC 0.75 TON  |
|  9  | AIR CONDITIONER   | SPLIT TYPE       | AC 1 TON     |
| 10  | AIR CONDITIONER   | WINDOWS TYPE     | WT 0.75 TON  |
| 11  | AIR CONDITIONER   | WINDOWS TYPE     | AC 1 TON     |
| 19  | NULL              | NULL             | BOOKS        |
| 18  | NULL              | NULL             | CDS          |
| 16  | NULL              | MUSIC PLAYERS    | CD PLAYERS   |
| 17  | NULL              | MUSIC PLAYERS    | MP3 PLAYERS  |
| 14  | NULL              | NULL             | PHOTO ALBUM  |
| 12  | TV                | LCD              | LCD 32" UP   |
| 13  | TV                | LCD              | LCD 42" UP   |
|  6  | NULL              | TV               | PLASMA       |
| 20  | NULL              | TV               | PORTABLE TV  |
+-----+-------------------+------------------+--------------+

这可以实现吗? 我可以使用 CASE WHEN 以及如何使用

或者我是否必须使用php重新排序结果?

THX

1 个答案:

答案 0 :(得分:1)

这与你想要的结果略有不同,但我认为这是你真正想要的:

SELECT (CASE WHEN l3.id IS NOT NULL THEN l3.id WHEN l2.id IS NOT NULL THEN l2.id ELSE l1.id END) as last_level_id, l1.name, l2.name, l3.name
FROM level l1
LEFT JOIN level l2 on l2.parentid = l1.id
LEFT JOIN level l3 on l3.parentid = l2.id
WHERE l1.parentid = 0
ORDER BY l1.id, l2.id, l3.id