2014-01-16 64 views
-1

,所以我有以下各表MySQL在左刪除重複使用ID

hobbies 
+----+-------------+---------------+ 
| id | employeeid | hobby_name | 
+----+-------------+---------------+ 
| 1 |   123 | cooking  | 
| 2 |   123 | painting  | 
| 3 |   124 | dancing  | 
+----+-------------+---------------+ 

nonacad_recog 
+----+-------------+---------------+ 
| id | employeeid | recog_name | 
+----+-------------+---------------+ 
| 1 |   123 | Award1  | 
| 2 |   123 | Award2  | 
| 3 |   124 | Award3  | 
+----+-------------+---------------+ 

org_membership 

+----+-------------+---------------+ 
| id | employeeid | recog_name | 
+----+-------------+---------------+ 
| 1 |   123 | Boyscout  | 
| 2 |   124 | Girlscout  | 
+----+-------------+---------------+ 

這是我使用的查詢加入3個表:

SELECT h.hobby_name,r.recog_name,o.org_name 
FROM hobbies as h 
JOIN nonacad_recog as r ON (h.employeeid=r.employeeid) 
JOIN org_membership as o ON (r.employeeid=o.employeeid) 
WHERE h.employeeid='123' 

我收到重複的輸出:

+----+-------------+---------------------+ 
| hobby_name | recog_name | org_name | 
+----+-------------+---------------------+ 
| cooking |   Award1 | Boyscout | 
| cooking |   Award2 | Boyscout | 
| painting|   Award1 | Boyscout | 
| painting|   Award2 | Boyscout | 
+----+-------------+---------------------+ 

我想作爲一個輸出是這樣的:沒有重複

+----+-------------+---------------------+ 
| hobby_name | recog_name | org_name | 
+----+-------------+---------------------+ 
| cooking |   Award1 | Boyscout | 
| painting|   Award2 | NULL  | 
+----+-------------+---------------------+ 

如果沒有東西可以返回,那麼也會給其他列/行賦予null。 我可能錯過了一些東西。

這是可以實現使用mysql查詢嗎? 任何解決方案?

如果我得到正確的表格,它會很容易讓我 mysqli_fetch將結果關聯起來,並使用php將其顯示在表格上。 謝謝。

+2

看看ORDER BY和DISTINCT –

回答

2

問題是,在希望的輸出中,您希望通過不存在的列(它是行號)將表中的行關聯起來。

技術上可以做到這樣的

SELECT MAX(CASE WHEN source = 1 THEN name END) hobby_name, 
     MAX(CASE WHEN source = 2 THEN name END) recog_name, 
     MAX(CASE WHEN source = 3 THEN name END) org_name 
    FROM 
(
    SELECT 1 source, id, hobby_name name, @n1 := @n1 + 1 rnum 
    FROM hobbies CROSS JOIN (SELECT @n1 := 0) i 
    WHERE employeeid = 123 
    UNION ALL 
    SELECT 2 source, id, recog_name, @n2 := @n2 + 1 rnum 
    FROM nonacad_recog CROSS JOIN (SELECT @n2 := 0) i 
    WHERE employeeid = 123 
    UNION ALL 
    SELECT 3 source, id, org_name, @n3 := @n3 + 1 rnum 
    FROM org_membership CROSS JOIN (SELECT @n3 := 0) i 
    WHERE employeeid = 123 
    ORDER BY source, id 
) q 
GROUP BY rnum 

輸出:

 
| HOBBY_NAME | RECOG_NAME | ORG_NAME | 
|------------|------------|----------| 
| cooking |  Award1 | Boyscout | 
| painting |  Award2 | (null) | 

這裏是SQLFiddle演示


這裏是一個替代的解決方案。你收拾所有的值作爲分隔字符串與GROUP_CONCAT()

SELECT GROUP_CONCAT(DISTINCT hobby_name ORDER BY h.id) hobby_name, 
     GROUP_CONCAT(DISTINCT recog_name ORDER BY r.id) recog_name, 
     GROUP_CONCAT(DISTINCT org_name ORDER BY o.id) org_name 
    FROM 
(
    SELECT 123 employeeid 
) e LEFT JOIN hobbies h 
    ON e.employeeid = h.employeeid 
    LEFT JOIN nonacad_recog r 
    ON e.employeeid = r.employeeid 
    LEFT JOIN org_membership o 
    ON e.employeeid = o.employeeid; 

輸出:

 
|  HOBBY_NAME | RECOG_NAME | ORG_NAME | 
|------------------|---------------|----------| 
| cooking,painting | Award1,Award2 | Boyscout | 

這裏是SQLFiddle演示

,然後在客戶端的PHP代碼容易explode()列值,而遍歷結果集並構建您的演示文稿。

+0

男人你是大!向你致敬...你能解釋一下你的代碼嗎......邏輯對我來說還不清楚.. – Murtaza

+0

HATS OFF!謝謝Mr.peterm。如果你只能向我們解釋。 – binaryjc

+0

你很受歡迎。我很高興我可以幫助:) – peterm

1

我認爲 「限制」 可以工作:
SELECT h.hobby_name,r.recog_name,o.org_name
FROM愛好爲h, nonacad_recog爲r, org_membership鄰
WHERE h.employeeid = '123'
和r.employeeid = o.employeeid
和h.employeeid = r.employeeid
LIMIT 1

+0

你沒有得到問題。無論如何,謝謝你的嘗試。 :) – binaryjc