mysql使用where获取重复的行

时间:2012-11-15 09:47:58

标签: php mysql sql

pid fid
6   19
7   19
8   19
9   19
7   30
6   30

我有一张这样的桌子。我想选择重复的行, 当我发送19个,30个ID时使用IN子句,如:

Select pid from tablename where fid IN (19,30);

我想要这些结果

pid
7
6

是否有任何mysql语句可以获得这些结果?

4 个答案:

答案 0 :(得分:3)

SELECT pid
FROM tableName
WHERE fid IN (19,30)
GROUP BY pid
HAVING COUNT(*) = 2

如果fid没有为每个pid定义唯一约束,那么您需要DISTINCT内的COUNT

SELECT pid
FROM tableName
WHERE fid IN (19,30)
GROUP BY pid
HAVING COUNT(DISTINCT fid) = 2

答案 1 :(得分:0)

使用distinct

Select distinct pid from tablename where fid IN (19,30);

或找到重复项

select pid from tablename group by fid having count(*)>1

答案 2 :(得分:0)

SELECT 
    pid 
FROM tablename 
where fid IN (19,30)
group by 
pid

答案 3 :(得分:0)

首先,你应该对pid进行分组并对它们进行计数,然后对多个出现的进行计数

SELECT pid, count(*) AS cnt
FROM tablename
WHERE fid IN (19,30)
GROUP BY pid
HAVING cnt > 1