使用mysql显示临时表中每个组的重复项

时间:2013-09-26 12:30:54

标签: mysql

有人可以使用mysql查询帮助我使用mysql显示临时表中每个组的重复项

sqlfiddle http://sqlfiddle.com/#!2/d8c9c/17

TOWN    ID1
town1   1
town1   1
town1   4
town2   1
town2   5
town2   8
town3   1
town3   3
town3   3

必填结果

TOWN    ID1
town1   1
town1   1
town3   3
town3   3

我已经通过TOWN,ID1尝试了SELECT * FROM Table1组 但这会删除重复项并显示非重复记录

2 个答案:

答案 0 :(得分:0)

这是您正在寻找的查询

SELECT TOWN, ID1  FROM Table1 t WHERE ( SELECT COUNT(*) FROM Table1 WHERE TOWN = t.TOWN AND ID1 = t.ID1 ) > 1;

答案 1 :(得分:0)

SELECT a.TOWN, a.ID1 FROM Table1 a JOIN
  (SELECT TOWN, ID1 FROM Table1 GROUP BY TOWN, ID1 HAVING COUNT(*) > 1) b 
ON a.TOWN = b.TOWN
AND a.ID1 = b.ID1