帮助形成 mysql 查询以查找给定日期范围内的免费(可用)场所/资源
我有桌子和像这样的数据:
venues table contains : id
+----+---------+ | id | name | +----+---------+ | 1 | venue 1 | | 2 | venue 2 | --------------- event_dates : id, event_id, event_from_datetime, event_to_datetime, venue_id +----+----------+---------------------+---------------------+----------+ | id | event_id | event_from_datetime | event_to_datetime | venue_id | +----+----------+---------------------+---------------------+----------+ | 1 | 1 | 2009-12-05 00:00:00 | 2009-12-07 00:00:00 | 1 | | 2 | 1 | 2009-12-09 00:00:00 | 2009-12-12 00:00:00 | 1 | | 3 | 1 | 2009-12-15 00:00:00 | 2009-12-20 00:00:00 | 2 | +----+----------+---------------------+---------------------+----------+
这是我的要求:我想要2009-12-06 00:00:00免费的场地 即
我应该得到
|venue_id|
|2 |
目前我有以下查询,
select ven.id , evtdt.event_from_datetime, evtdt.event_to_datetime from venues ven left join event_dates evtdt on (ven.id=evtdt.venue_id) where evtdt.venue_id is null or not ('2009-12-06 00:00:00' between evtdt.event_from_datetime and evtdt.event_to_datetime); +----+---------------------+---------------------+ | id | event_from_datetime | event_to_datetime | +----+---------------------+---------------------+ | 1 | 2009-12-09 00:00:00 | 2009-12-12 00:00:00 | | 2 | 2009-12-15 00:00:00 | 2009-12-20 00:00:00 | | 3 | NULL | NULL | | 5 | NULL | NULL | +----+---------------------+---------------------+
如果您注意到结果,它不包括日期在 2009-12-06 00:00:00 之间的场地 id 1,但显示其他预订。 请帮我纠正这个问题。
提前致谢。
I have tables & data like this:
venues table contains : id
+----+---------+ | id | name | +----+---------+ | 1 | venue 1 | | 2 | venue 2 | --------------- event_dates : id, event_id, event_from_datetime, event_to_datetime, venue_id +----+----------+---------------------+---------------------+----------+ | id | event_id | event_from_datetime | event_to_datetime | venue_id | +----+----------+---------------------+---------------------+----------+ | 1 | 1 | 2009-12-05 00:00:00 | 2009-12-07 00:00:00 | 1 | | 2 | 1 | 2009-12-09 00:00:00 | 2009-12-12 00:00:00 | 1 | | 3 | 1 | 2009-12-15 00:00:00 | 2009-12-20 00:00:00 | 2 | +----+----------+---------------------+---------------------+----------+
This is my requirement: I want venues that will be free on 2009-12-06 00:00:00
i.e.
I should get
|venue_id|
|2 |
Currently I'm having the following query,
select ven.id , evtdt.event_from_datetime, evtdt.event_to_datetime from venues ven left join event_dates evtdt on (ven.id=evtdt.venue_id) where evtdt.venue_id is null or not ('2009-12-06 00:00:00' between evtdt.event_from_datetime and evtdt.event_to_datetime); +----+---------------------+---------------------+ | id | event_from_datetime | event_to_datetime | +----+---------------------+---------------------+ | 1 | 2009-12-09 00:00:00 | 2009-12-12 00:00:00 | | 2 | 2009-12-15 00:00:00 | 2009-12-20 00:00:00 | | 3 | NULL | NULL | | 5 | NULL | NULL | +----+---------------------+---------------------+
If you note the results, its not including venue id 1 where date is in between 2009-12-06 00:00:00 but showing other bookings.
Please help me correct this query.
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
2009 年 12 月 6 日介于 12/5/09 和 12/7/09 之间...这就是为什么venue_id 1被排除...您试图从数据中提取什么确切地?
您构建的联接查询表示,获取场地表,并为其中具有匹配的venue_id 的每一行制作场地表行的副本并附加匹配的行。因此,如果您刚刚这样做:
它将产生:
如果您随后添加了条件,该条件指出感兴趣的日期不在事件的起始日期和结束日期之间,则查询如下所示:
产生的结果为:
这些是我的实际结果使用 MySQL 中的数据进行实验结果。
如果您想获得在建议日期免费的venue_ids,那么您可以编写类似以下内容的内容:
其结果是:
如果您的场地在 event_date 中没有任何活动,这也会得出正确的答案(无需修改)桌子。
12/6/2009 is between 12/5/09 and 12/7/09... that's why venue_id 1 is being excluded... what is it you're trying to extract from the data exactly?
The join query you've constructed says, take the venues table and for each row of it that has a matching venue_id make a copy of the venue table row and append the matching row. So if you just did:
It would yield:
If you then added your condition, which states the date of interest is not between the from and to date of the event, the query looks like:
Which yields a result of:
These are my actual experimental results with your data in MySQL.
If you want to get the venue_ids that are free on the proposed date then you would write something like:
which yields:
this also comes up with the right answer (without modification) in the case where you have a venue with no events in the event_date table.
据猜测,如果您从中删除not,
则会返回事件日期的第 1 行,但不会返回其他事件日期行。
我说“猜测”是因为你的 where 子句有点难以理解。也许你的意思是
At a guess, if you remove not from
this will then return row 1 from event dates but not the other event date rows.
I say "at a guess" because your where clause is a bit hard to understand. Maybe you mean