Reputation: 14904
I have the following setup:
Now ill have an array of categories, and ill want to show the items from table 1 (news for example) that fits to ALL categories. So if ill have ID 35 and ID 36, there must be an entry for both IDs in the connection table.
For example:
SELECT *
FROM inserate
INNER JOIN rubrikenZuordnungen ON inserate.ID = rubrikenZuordnungen.InseratID
WHERE
(rubrikenZuordnungen.RubrikID = 35) AND
(rubrikenZuordnungen.RubrikID = 36)
OFFSET 10 ROWS
FETCH NEXT 20 ROWS ONLY
If I'll use that, it's not working because I'll get only 1 result for rubrikenZuordnungen
.
Can anyone help me out? Thanks in advance.
Upvotes: 1
Views: 42
Reputation: 452978
This is one way.
SELECT *
FROM inserate
WHERE ID IN
(
SELECT InseratID
FROM rubrikenZuordnungen
WHERE RubrikID IN (35,36)
GROUP BY InseratID
HAVING COUNT(DISTINCT RubrikID)=2
)
Upvotes: 2