2011-07-01 43 views
0

選擇的值堆棧我有這樣的查詢:它是更多鈔票用MYSQL GROUP BY

SELECT email 
FROM abc_table 
GROUP BY email 
HAVING (COUNT(email) > 1) 

那麼它將返回我:

email 
[email protected]  
[email protected]  
[email protected] 

,現在我需要調整查詢得到的東西是這樣的:

email  id 
[email protected]  1 
[email protected]  2 
[email protected]  3 
[email protected]  4 
[email protected]  5 
[email protected]  6 

它是更多鈔票來得到這樣的結果,通過使用GROUP BY HAVING?或者有什麼建議可以得到這個結果?

非常感謝!

+1

我猜你有一個更一行'([email protected],7)'什麼地方? –

+0

不知道你的表中有什麼,樣品沒有意義。看起來你想刪除'GROUP BY'? –

回答

0

使用GROUP_CONCAT:

SELECT email, GROUP_CONCAT(id) FROM abc_table GROUP BY email 

(編輯:誤讀的問題)

+0

謝謝,你的方法很好:) –

2
SELECT a.email 
    , a.id 
FROM abc_table a 
    JOIN 
    (SELECT email 
     FROM abc_table 
     GROUP BY email 
     HAVING COUNT(email) > 1 
    ) AS ag 
    ON ag.email = a.email 
+0

感謝您的建議。 :) –