KeironLowe
KeironLowe

Reputation: 731

MySQL - Get row if other rows match

entry_id                 cat_id
----------------         ----------------
1                        1
1                        2
1                        3
2                        2
3                        4

I know the title isn't very helpful, but I basically, I need to return the entry ID of a row, if certain categories are set.

So for example I need to return the entry_id which has the a cat_id of 1, and either 2 or 3.

Would be nice if we could avoid duplicates results, but I can always remove duplicates with PHP after if it's not possible.

Help is really appreciated.

Upvotes: 0

Views: 42

Answers (2)

Barmar
Barmar

Reputation: 781096

SELECT DISTINCT t1.entry_id
FROM yourTable t1
JOIN yourTable t2 ON t1.entry_id = t2.entry_id
WHERE t1.cat_id = 1
AND t2.cat_id IN (2, 3)

Upvotes: 3

echo_Me
echo_Me

Reputation: 37233

Try this:

  SELECT  entry_id
  FROM table1 
  WHERE cat_id = 1
  OR cat_id IN (2, 3)
  GROUP BY entry_id

Since its unclear what you are asking, I will give other attempt:

 SELECT  entry_id
 FROM table1 
 WHERE cat_id = 1
 AND   (cat_id = 2 or cat_id = 3)
 GROUP BY by entry_id

Upvotes: 0

Related Questions