Pramod J George
Pramod J George

Reputation: 1723

Combining results from two queries in mysql

I have a table like the one as below:

 id     friends_id  
 4      2
 4      3
 5      3

I need the friends_id from this table common to the id values 4 and 5. So the query must return only the value 3 common to bot 4 and 5 .

friends_id
    3

How can I achieve this in "MYSQL".

Upvotes: 0

Views: 33

Answers (1)

John Woo
John Woo

Reputation: 263913

SELECT  friends_id
FROM    TableName
WHERE   id IN (4, 5)
GROUP   BY friends_id
HAVING   COUNT(DISTINCT id) = 2

Assuming, however, id is unique, you can omit DISTINCT

HAVING   COUNT(*) = 2

Upvotes: 1

Related Questions