0
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

4 回答 4

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
于 2012-11-15T09:49:23.620 回答
0

利用distinct

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

或查找重复项

select pid from tablename group by fid having count(*)>1
于 2012-11-15T09:48:51.313 回答
0

首先,您应该对pid进行分组并计数,然后取出出现多次的pid

SELECT pid, count(*) AS cnt
FROM tablename
WHERE fid IN (19,30)
GROUP BY pid
HAVING cnt > 1
于 2012-11-15T09:54:41.877 回答
0
SELECT 
    pid 
FROM tablename 
where fid IN (19,30)
group by 
pid
于 2012-11-15T09:50:49.623 回答