检查一张表中的日期在第二张表的预订期间不存在
在花了两天时间试图在网上找到答案后,我对此很陌生 我正在尝试构建一个 MYSQL select 语句,
我在 e 上有 2 个表,其中包含日期和价格列表,第二个是包含开始和结束日期的预订日期表。我想向大家展示免费的日期和时间。表 1 中的价格不存在于表 2 中的任何预订记录中
表 1 名为 - dates_prices -
id、日期、价格
示例数据
333, 2011-12-20, 66.00
333, 2011-12-21, 66.00
333, 2011-12-22, 66.00
333, 2011-12-23, 66.00
333, 2011-12-24, 66.00
333, 2011-12-25, 66.00
333, 2011-12-26, 66.00
333, 2011-12-27, 66.00
333, 2011-12-28, 66.00
333, 2011-12-29, 66.00
333, 2011-12-30, 66.00
表 2 - 预订
id、开始日期、结束日期
示例数据
333, 2011-12-20, 2011-12-22
333, 2011-12-24, 2011-12-26
333, 2011-12-28, 2011-12-30
我只需要从表 1 中提取表 2 中不存在的日期表 2 中具有相同 ID 号的所有记录的开始日期和结束日期之间的记录 333
因此,表 1 中应显示的唯一记录如下
id、日期、价格
333, 2011-12-23, 66.00
333, 2011-12-27, 66.00
333, 2011-12-31, 66.00
I am new to this after spending 2 days trying to find an answer on the net
I am trying to construct a MYSQL select statement
I have 2 tables on ethat contains a list of dates and prices, the second is a table of reservation dates with start and end. I am tring to show al the FREE dates & prices in Table 1 that DO NOT exist in any of the reservation records in Table 2
Table 1 called - dates_prices -
id, date, price
example data
333, 2011-12-20, 66.00
333, 2011-12-21, 66.00
333, 2011-12-22, 66.00
333, 2011-12-23, 66.00
333, 2011-12-24, 66.00
333, 2011-12-25, 66.00
333, 2011-12-26, 66.00
333, 2011-12-27, 66.00
333, 2011-12-28, 66.00
333, 2011-12-29, 66.00
333, 2011-12-30, 66.00
Table 2 - reservations
id, startdate, enddate
example data
333, 2011-12-20, 2011-12-22
333, 2011-12-24, 2011-12-26
333, 2011-12-28, 2011-12-30
I need to extract ONLY the dates from Table 1 that do not exist in the Table 2 records between start and end dates of all records in table 2 with the same ID number 333
So the only records that should be displayed from table 1 are as follows
id, date, price
333, 2011-12-23, 66.00
333, 2011-12-27, 66.00
333, 2011-12-31, 66.00
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我已更新查询以排除日期。
I've updated the query to exclude dates.
创建上述表格后尝试此操作。您的问题的解决方案是您需要对表进行这种类型的查询,即
select * from table12 其中 datefor 不在“2011-12-20”和“2011-12-22”之间,datefor 不在“2011-12-24”和“2011-12-26”之间,datefor 不在“2011-12”之间-28' 和 '2011-12-30' 和 table12.id=333 我做了一个过程只是你需要传递你的ID。
try this after creating above tables. solution to ur probelm is u need to make this type of query to tables ie
select * from table12 where datefor not between '2011-12-20' and '2011-12-22' and datefor not between '2011-12-24' and '2011-12-26' and datefor not between '2011-12-28' and '2011-12-30' and table12.id=333 i have made one proc just u need to pass ur id.
这也应该有效,可能会快得多:
This should work too, could be a lot faster: