- Możesz
GROUP BY
na identyfikatorze i nazwie użytkownika. - Użyj
HAVING
klauzula zSUM()
agregacja w celu odfiltrowania przypadków. - Jeśli użytkownik nie pracuje w określonym dniu,
SUM()
dla tego dnia będzie zero, opublikuj połączenia.
W pierwszym przypadku (działa dokładnie we wszystkie dni robocze tygodnia), Wypróbuj:
SELECT u.id, u.name
FROM USERS AS u
JOIN WORKDAYS AS wd ON wd.user_id = u.id
JOIN DAYS AS d ON d.id = wd.day_id
GROUP BY u.id, u.name
HAVING SUM(d.name = 'monday')
AND SUM(d.name = 'tuesday')
AND SUM(d.name = 'wednesday')
AND SUM(d.name = 'thursday')
AND SUM(d.name = 'friday')
AND SUM(d.name = 'sunday') = 0
AND SUM(d.name = 'saturday') = 0
W drugim przypadku po prostu usuń warunki w sunday
i saturday
. Wypróbuj:
SELECT u.id, u.name
FROM USERS AS u
JOIN WORKDAYS AS wd ON wd.user_id = u.id
JOIN DAYS AS d ON d.id = wd.day_id
GROUP BY u.id, u.name
HAVING SUM(d.name = 'monday')
AND SUM(d.name = 'tuesday')
AND SUM(d.name = 'wednesday')
AND SUM(d.name = 'thursday')
AND SUM(d.name = 'friday')