我正在努力让MySQL给我所有在某个时间没有预订的教练。此查询本质上是搜索查询。因此,请回复所有尚未预订的教师及其详细信息。有人可以帮我完成它。这让我疯了。
SELECT
AddressTypes.AddressTypeName,
Addresses.*,
InstructorSettings.*,
Users.*,
BookedSlots.DateTime
FROM Users
LEFT OUTER JOIN InstructorSettings
ON Users.UserID = InstructorSettings.UserID
LEFT OUTER JOIN Addresses
ON Users.UserID = Addresses.UserID
INNER JOIN AddressTypes
ON Addresses.AddressTypeID = AddressTypes.AddressTypeID
LEFT OUTER JOIN BookedSlots
ON Users.UserID = BookedSlots.UserID
WHERE Users.AccountTYpe = 3 AND Addresses.PostCode1 IN ('l13') AND BookedSlots.DateTime <> '2013-04-25 11:00:00'
上面的查询不会返回任何内容,但如果我取出“AND BookedSlots.DateTime&lt;&gt;'2013-04-25 11:00:00'”,它会返回所有教师详细信息。
数据库
非常感谢任何帮助。
答案 0 :(得分:0)
在我看来,您所寻找的是指定时间内BookedSlots中缺少记录。
我不确定这是否被视为MySQL最佳做法,但我通常不是使用join
来完成该任务,而是使用子选择或临时表,如下所示:
SELECT
AddressTypes.AddressTypeName,
Addresses.*,
InstructorSettings.*,
Users.*,
FROM Users
LEFT OUTER JOIN InstructorSettings
ON Users.UserID = InstructorSettings.UserID
LEFT OUTER JOIN Addresses
ON Users.UserID = Addresses.UserID
INNER JOIN AddressTypes
ON Addresses.AddressTypeID = AddressTypes.AddressTypeID
WHERE Users.AccountType = 3 AND Addresses.PostCode1 IN ('l13')
AND Users.UserID not in (SELECT BookedSlots.InstructorID FROM BookedSlots
WHERE BookedSlots.DateTime = '2013-04-25 11:00:00')
编辑:根据评论,更改select语句以废除BookedSlots,并选择子选项以返回InstructorId(在预订时与教师而不是学生匹配)。
答案 1 :(得分:0)
要检查特定时间段上是否有任何预订,请在连接条件中包含时间段,并选择时间段ID为空的位置 - 如下所示:
SELECT
AddressTypes.AddressTypeName,
Addresses.*,
InstructorSettings.*,
Users.*,
BookedSlots.DateTime
FROM Users
LEFT OUTER JOIN InstructorSettings
ON Users.UserID = InstructorSettings.UserID
LEFT OUTER JOIN Addresses
ON Users.UserID = Addresses.UserID
INNER JOIN AddressTypes
ON Addresses.AddressTypeID = AddressTypes.AddressTypeID
LEFT OUTER JOIN BookedSlots
ON Users.UserID = BookedSlots.UserID AND
BookedSlots.DateTime = '2013-04-25 11:00:00'
WHERE Users.AccountTYpe = 3 AND
Addresses.PostCode1 IN ('l13') AND
BookedSlots.UserID is null