从表中选择所有重复项

时间:2013-02-17 14:18:43

标签: mysql sql select

我有一个包含这样的列的表:

serial_id   season  ep_id
1           1       1   < duplicate
1           1       2
3           1       1
...
1           1       1   < duplicate

我想在serial_id,season和ep_id中选择所有具有重复值的行,我应该使用哪个查询? 感谢。

2 个答案:

答案 0 :(得分:2)

“.. serial_id,season和ep_id中的重复值。”

SELECT  serial_id, ep_id, season
FROM    tableName
GROUP   BY serial_id, ep_id, season
HAVING  COUNT(*) > 1

但是如果你想获得所有列(*假设除了serial_id,ep_id,season *之外还有其他列)

SELECT  a.*
FROM    tableName a
        INNER JOIN
        (
            SELECT  serial_id, ep_id, season
            FROM    tableName
            GROUP   BY serial_id, ep_id, season
            HAVING  COUNT(*) > 1
        ) b ON  a.serial_id = b.serial_id AND
                a.ep_id = b.ep_id AND
                a.season = b.season

答案 1 :(得分:1)

类似的东西:

select serial_id, season, ep_id, count(*) records
from yourtable
group by serial_id, season, ep_id
having count(*) > 1