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

I have a table like this. I want to select duplicate rows, when I'm sending 19, 30 ids Using an IN clause like:

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

I want these results

pid
7
6

Is there any mysql statement to get these results?

0

4 Answers 4

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

if unique constraint was not defined on fid for each pid, then you need to have DISTINCT inside COUNT

SELECT pid
FROM tableName
WHERE fid IN (19,30)
GROUP BY pid
HAVING COUNT(DISTINCT fid) = 2
Sign up to request clarification or add additional context in comments.

Comments

0

Use distinct

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

or to find the duplicates

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

Comments

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

Comments

0

First, you should group pid's and count them, and then take the ones with more than one occurence

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.