I have a Symfony project with 2 entities like Session 1 <-> n Timeslots
, (and many more other entities).
My problem is i can't find a query that would retrieve the list of Sessions where ALL the timeslots are greater than a given date. Is it possible to do it in one query?
I've tried with MIN (simplified version, without the extra JOIN with other entities):
SELECT s.id
FROM session s
JOIN timeslot t ON t.sessionId = s.id
HAVING MIN(t.timelotDate) > '2019-05-05';
But i don't retrieve any element with this query.
I've made a sqlfiddle for it:
http://www.sqlfiddle.com/#!9/4b485b/5
use max()
and group by
SELECT s.id
FROM session s
JOIN timeslot t ON t.sessionId = s.id group by s.id
having max(t.timelotDate) > '2019-05-05'
You forgot GROUP BY
:
SELECT session.id
FROM session
JOIN timeslot ON timeslot.sessionId = session.id
GROUP BY session.id
HAVING MIN(timeslot.timeslotDate) > '2019-05-05'
An alternate would be:
SELECT *
FROM session
WHERE NOT EXISTS (
SELECT 1
FROM timeslot
WHERE timeslot.sessionId = session.id
AND timeslot.timeslotDate <= '2019-05-05'
)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.