0
表名的ID:水平找出只有那些沒有孩子ORDER BY父母和祖父母
+-----+-------------------+----------+
| 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>
所以我需要找出一個不同的查詢:
的處置RESULT
+-----+-------------------+------------------+--------------+
| 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何時以及如何?
或者我必須使用PHP重新排序結果?
THX
是的,我認爲可以從那裏! –