Answer the question
In order to leave comments, you need to log in
How to correctly build a query to two tables?
There are 2 tables:
first:
UserID - Cat - Group
1200 - 1 - 3
1200 - 4 - 20
1200 - 2 - 7
1235 - 1 - 2
1235 - 4 - 20
1450 - 1 - 3
second:
UserID - UserName
1200 - Ivan
1235 - Peter
1450 - Dima It is
necessary to create a query that will return the username of the user included in all specified Category-Group pairs.
At the moment I'm using the following structure:
SELECT
COUNT(UserID)
, UserName
FROM usertable ut
INNER JOIN catgrouptable cg ON ut.UserID = cg.UserID
AND (
(cg.cat = 1 AND cg.group IN (3,10,9))
OR (cg.cat = 4 AND cg.group IN (1,2,3,20))
-- неопределенное количество условий вхождения в группы и категории
)
GROUP BY UsetName
HAVING COUNT(UserID)
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question