In my MySQL database I have two columns setup like this (for example):
series_id category_id
1 1
1 2
1 3
2 1
2 3
3 2
4 1
4 2
What is the proper select query I can use to select the series_id when the category_id = 1 and 2?
Current query is this:
SELECT series_id
FROM table
WHERE category_id = '1'
OR category_id = '2'
GROUP BY series_id"
This does not retrieve any results:
SELECT series_id
FROM table
WHERE category_id = '1'
AND category_id = '2'
GROUP BY series_id
In regards to this example I would like it to return the two rows where the series_id is 1 and 2 so the result looks like this:
series_id
1
4
(Optional / Bonus) If it is possible to come up with another solution or utilize:
GROUP_CONCAT(category_id SEPARATOR ', ') AS category
for the returned series_id (s) from this query so the returned result is:
series_id category
1 1, 2, 3
4 1, 2
See it on sqlfiddle.
Note that if
(series_id, category_id)are not guaranteed to be unique, you should replaceCOUNT(*)with the less performantCOUNT(DISTINCT category_id).To return the grouped results, you can join against your table again:
See it on sqlfiddle.