选择过滤器

时间:2011-11-29 15:52:11

标签: sql database ado

我正在为诊所开发预约系统,所以我的目标是我想显示尚未预约的医生班次,患者检查特定医生在特定日期和班次的可用性。
我有2张桌子

Booking (ID, DocID, shift_id, Date)
Shift (ID, name, DocID) 

MySQL选择不起作用的查询是:

SELECT  Shift.ID, Shift.name, Shift.DocID
FROM    Shift INNER JOIN
        Booking ON Shift.ID = Booking.shift_id
WHERE   (Shift.DocID = @DoctorID) AND (Booking.DocID <> @DoctorID) 
 AND    (Booking.shift_id <> @ShiftID) AND (Booking.Date <> @VisitDate)

所以请任何想法???

2 个答案:

答案 0 :(得分:1)

你可以尝试

SELECT s.* FROM Shift
LEFT JOIN Booking b 
    ON s.id = b.shift_id
   AND s.DocID = b.DocID
WHERE b.id IS NULL
   OR b.date <> visit_date

答案 1 :(得分:0)

左连接方法:

SELECT s.* FROM Shift s
LEFT JOIN Booking b 
    ON s.id = b.shift_id
   AND s.DocID = b.DocID
   AND b.date = @visit_date
WHERE s.ID = @ShiftID and s.DocID = @DocID and b.id IS NULL

您也可以使用NOT EXISTS或NOT IN条款来编写变体 - 关键是您希望在那天没有预约的情况下进行轮班,当天的班次