2012-11-15 61 views
0
pid fid 
6 19 
7 19 
8 19 
9 19 
7 30 
6 30 

我有一個這樣的表。我想選擇重複行, 當我送19,30個IDS使用的IN條款,如:mysql得到重複行使用哪裏

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

我想這些結果

pid 
7 
6 

是否有任何MySQL的語句來得到這些結果?

+0

爲什麼不是6 7 8 9? –

回答

0

使用distinct

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

或者找到重複

select pid from tablename group by fid having count(*)>1 
3
SELECT pid 
FROM tableName 
WHERE fid IN (19,30) 
GROUP BY pid 
HAVING COUNT(*) = 2 

如果唯一約束是不fid每個pid定義,那麼你需要有DISTINCTCOUNT

SELECT pid 
FROM tableName 
WHERE fid IN (19,30) 
GROUP BY pid 
HAVING COUNT(DISTINCT fid) = 2 
0
SELECT 
    pid 
FROM tablename 
where fid IN (19,30) 
group by 
pid 
0

首先,你應該組PID的並計數,然後拿那些與多個occurence

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