简体   繁体   English

MySQL连接选择两个表之间的最小(日期)和最大(日期)

[英]mysql join select min(date) and max (date) between two tables

I have two tables in my database one is for venue and the other is for booking. 我的数据库中有两个表,一个用于场地,另一个用于预订。 what I need is a query to select all the venue that are not booked. 我需要的是查询以选择所有尚未预订的场所。 See below: 见下文:

table booking has the following fields: 表格预订包含以下字段:

  • bk_id bk_id

  • venue_id as a foreign key from (booking table) 场所ID作为预订表中的外键

  • start_date (as date type ) //indicate when the venue wouldn't be start_date(日期类型)//指示何时不使用该地点

  • available for booking end_date (as date type )//indicating the end date for the booking and from here onward the venue would be available for booking 可用于预订end_date(作为日期类型)//指示预订的结束日期,从此处开始,场地将可供预订

and venue table has following fields: 地点表具有以下字段:

  • venue_id as a primary key etc. 会场ID作为主键等。

What I need is to be able to search for all the venue that are not booked. 我需要的是能够搜索所有尚未预订的场所。 How should I set my mysql query 我应该如何设置我的MySQL查询

For venue not booked on particular date use 对于未在特定日期预订的场地,请使用

SELECT * FROM venue WHERE venue_id NOT IN (SELECT venue_id FROM booking WHERE {YOUR_DATE} BETWEEN  start_date AND end_date)

For venues that are never booked 对于从未预订的场所

SELECT * FROM venue WHERE venue_id NOT IN (SELECT venue_id FROM booking)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM