2014-03-24 102 views
0

我有這樣的代碼,檢查有多少重複的代碼有:添加額外的數據重複記錄

mysql> SELECT code, count(code) as dup from tg_user group by code having dup>1 order by dup; 

這將返回:

+------------+-----+ 
| CCC002  | 5 | 
| BAR003  | 6 | 
| FIR001  | 6 | 
| NJS001  | 6 | 
| DEL004  | 6 | 
| BRA009  | 7 | 
| TSH011  | 11 | 
| SHO005  | 19 | 
+------------+-----+ 
432 rows in set (0.08 sec) 

表結構是:

mysql> describe tg_user; 
+---------------+--------------+------+-----+---------+----------------+ 
| Field   | Type   | Null | Key | Default | Extra   | 
+---------------+--------------+------+-----+---------+----------------+ 
| user_id  | int(11)  | NO | PRI | NULL | auto_increment | 
| user_name  | varchar(30) | YES | UNI | NULL |    | 
| email_address | varchar(255) | YES | UNI | NULL |    | 
| code   | varchar(25) | YES |  | NULL |    | 
+---------------+--------------+------+-----+---------+----------------+ 
32 rows in set (0.00 sec) 

什麼是爲每個重複記錄添加email_address的正確方法,以便它成爲一個列表,類似於:

| SHO005  | 19 | [email protected], [email protected]  | 
+------------+-----+-----------------------------------------+ 
432 rows in set (0.08 sec) 

任何意見大加讚賞添加電子郵件地址只添加選擇條款和補充條款中組現場要花最少的時間

回答

0

GROUP_CONCAT用逗號連接的值。 添加DISTINCT以過濾出重複項

SELECT code, count(code) as dup, GROUP_CONCAT(DISTINCT email_address) 
from tg_user group by code having dup>1 order by dup; 
0

的最佳方式。

,如: -

SELECT code, count(code) as dup ,email_address from tg_user 
group by code,email_address having dup>1 order by dup;