我们的数据库中有一系列预订,每个预订都有一个多个会话序列。每个会话都有一个“停止”,其中包含一个DateTime。
我想选择所有会话在两个日期之间结束的预订。
SELECT DISTINCT b.*
FROM Booking as b
JOIN Sequence as se
JOIN Session as s
ON (b.sequence_id=se.id AND s.sequence_id=se.id
AND s.stop between '2012-09-01 00:00:00' AND '2012-12-01 00:00:00');
此SQL选择包含一个或多个已结束会话的预订。
我只想要所有会话结束的预订。
有什么想法吗?
答案 0 :(得分:2)
thanx,确切的查询是这样的:
SELECT b.id, s.stop
FROM Booking as b
JOIN Sequence as se
JOIN Session as s
ON (b.sequence_id=se.id AND s.sequence_id=se.id)
where not exists (SELECT ss.id from Session as ss where ss.sequence_id=se.id and ss.stop < '2012-09-01 00:00:00')
and not exists (SELECT ss.id from Session as ss where ss.sequence_id=se.id and ss.stop > '2012-12-01 00:00:00');
答案 1 :(得分:1)
我想知道你是否可以这样做:
SELECT DISTINCT b.*
FROM Booking as b
JOIN Sequence as se
JOIN Session as s
ON (b.sequence_id=se.id AND s.sequence_id=se.id )
where s.stop between '2012-09-01 00:00:00' AND '2012-12-01 00:00:00')
and not exists ( s.stop < '2012-09-01 00:00:00' )
and not exists ( s.stop > '2012-12-01 00:00:00');
虽然语法不太正确。