- Puedes
GROUP BY
en la identificación y el nombre del usuario. - Usar
HAVING
cláusula conSUM()
agregación para filtrar casos. - Si un usuario no trabaja en un día en particular,
SUM()
para ese día será cero, se une a la publicación.
Para el primer caso (funciona exactamente todos los días laborables de la semana), Pruebe:
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
Para el segundo caso, simplemente elimine las condiciones el sunday
y saturday
. Prueba:
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')