- Vous pouvez
GROUP BY
sur l'identifiant et le nom de l'utilisateur. - Utilisez
HAVING
clause avecSUM()
agrégation pour filtrer les cas. - Si un utilisateur ne travaille pas un jour particulier,
SUM()
pour ce jour sera zéro, publier des jointures.
Pour le premier cas (fonctionne exactement tous les jours ouvrés de la semaine), essayez :
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
Pour le deuxième cas, supprimez simplement les conditions le sunday
et saturday
. Essayez :
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')