我有这个代码检查有多少重复代码:
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 | name@domai.tld, name@domai.tld... |
+------------+-----+-----------------------------------------+
432 rows in set (0.08 sec)
任何建议非常感谢
答案 0 :(得分:0)
添加电子邮件地址的最佳方法是添加select子句,并在group by子句中添加该字段,花费的时间最短。
喜欢: -
SELECT code, count(code) as dup ,email_address from tg_user
group by code,email_address having dup>1 order by dup;
答案 1 :(得分: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;