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语句可以获得这些结果?
答案 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