我有一个超过50k的数据表,其中有很多重复项。需要列出所有具有重复名称的字段。
这是我的桌子
id | name | comid | did
--------------------------
1 | sahbaj | 1 | 1
---------------------------
2 | sahbaj | 2 | 2
---------------------------
3 | rafi | 3 | 3
---------------------------
4 | zaaef | 4 | 34
---------------------------
5 | promi | 5 | 5
---------------------------
6 | sanu | 6 | 5
---------------------------
7 | rafi | 7 | 3
---------------------------
8 | sd | 43 | 8
---------------------------
9 | sd | 6 | 9
我的目标输出
id | name | did -------------------------- 1 | sahbaj | 1 -------------------------- 2 | sahbaj | 2 -------------------------- 8 | sd | 8 -------------------------- 9 | sd | 9 --------------------------
我已经尝试过使用此MySQL查询
SELECT id,u.name, u.did FROM test u INNER JOIN ( SELECT NAME,did, COUNT(*) FROM test GROUP BY NAME HAVING COUNT(*) > 1) temp ON (temp.name = u.name) ORDER BY NAME
哪个给我以下输出
id | name | did -------------------------- 3 | rafi | 3 -------------------------- 7 | rafi | 3 -------------------------- 1 | sahbaj | 1 -------------------------- 2 | sahbaj | 2 -------------------------- 8 | sd | 8 -------------------------- 9 | sd | 9 --------------------------
也试过以下
SELECT id, name, did FROM test t WHERE EXISTS (SELECT 1 FROM test t2 WHERE t2.name = t.name AND t2.did t.did)
它给出目标输出,但是如果我在实际表中运行它,那么它将持续无穷时间
答案 0 :(得分:0)
我会这样做
select id, name, did from test
where name in (select name from test group by name having count(*) > 1)
答案 1 :(得分:0)
您不应在内部查询中使用did,而应仅使用名称
SELECT ud.1, u.name, u.did
FROM test u
INNER JOIN (
SELECT name, COUNT(*)
FROM test
GROUP BY name
HAVING COUNT(*) > 1
) t ON t.name= u.name
ORDER BY u.name
答案 2 :(得分:0)
我找到了以下解决方案
首先,我在MySQL创建视图
CREATE VIEW duplicates AS (SELECT u.id,u.name, u.did FROM test u INNER JOIN ( SELECT NAME,did, COUNT(*) FROM test GROUP BY NAME HAVING COUNT(*) > 1) temp ON (temp.name = u.name) ORDER BY name);
然后运行此查询
SELECT p.id, p.name,p.did FROM test AS p INNER JOIN duplicates AS d ON (d.name=p.name AND d.did!=p.did);