我有两张桌子,
prices (pcode, date, priceperweek)
booked (pcode, date)
表单由3个字段组成
以下查询应选择价格&日期并检查价格表中是否显示所选的开始日期,并且从开始日期开始的每一天都没有出现在预订表中。
SELECT SUM(priceperweek) AS `ppw`, prices.date AS `startdate`
FROM `prices` LEFT JOIN `booked` ON prices.pcode=booked.pcode
WHERE prices.pcode='A2CD59GH'
AND (prices.date IN ('20131221', '20131228')
AND booked.date NOT IN ('20131221', '20131222', '20131223',
'20131224', '20131225', '20131226', '20131227', '20131228',
'20131229', '20131230', '20131231', '20140101', '20140102',
'20140103')
)
OR (prices.date IN ('20131214', '20131221')
AND booked.date NOT IN ('20131214', '20131215', '20131216',
'20131217',
'20131218', '20131219', '20131220', '20131221', '20131222',
'20131223', '20131224', '20131225', '20131226', '20131227')
)
OR (prices.date IN ('20131228', '20140104') AND booked.date NOT IN
('20131228', '20131229', '20131230', '20131231', '20140101',
'20140102', '20140103', '20140104', '20140105', '20140106',
'20140107', '20140108', '20140109', '20140110')
)
GROUP BY prices.date
ORDER BY prices.date ASC
有价值的评论......
我的问题是,即使某个范围中的某些日期出现在“预订”表中,此查询也会返回记录,并且ppw值比我预期的要多。
使用SUM(ppw)的原因是当指定持续时间为14时,价格将在两个星期内合并。
感谢您对此提供的任何帮助
答案 0 :(得分:1)
您的方法存在的问题是startdate
只会在给定booked
的{{1}}表格中的每条记录中从结果中过滤掉在预订期限内。显然,如果该物业已在其他日期被预订,情况就不会如此。
我建议按以下方式执行反连接:
pcode