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 语句来获得这些结果?
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
利用distinct
Select distinct pid from tablename where fid IN (19,30);
或查找重复项
select pid from tablename group by fid having count(*)>1
首先,您应该对pid进行分组并计数,然后取出出现多次的pid
SELECT pid, count(*) AS cnt
FROM tablename
WHERE fid IN (19,30)
GROUP BY pid
HAVING cnt > 1
SELECT
pid
FROM tablename
where fid IN (19,30)
group by
pid