检查一张表中的日期在第二张表的预订期间不存在

发布于 2024-12-22 16:06:55 字数 941 浏览 2 评论 0原文


在花了两天时间试图在网上找到答案后,我对此很陌生 我正在尝试构建一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

哽咽笑 2024-12-29 16:06:55
SELECT p1.* FROM dates_prices p1 
WHERE (p1.id, p1.date) NOT IN
(
  SELECT p2.id, p2.date
  FROM dates_prices p2
  JOIN reservations r
  ON  (
     r.id = p2.id 
     and r.startdate <= p2.date 
     and p2.date <= r.enddate
  )
)
SELECT p1.* FROM dates_prices p1 
WHERE (p1.id, p1.date) NOT IN
(
  SELECT p2.id, p2.date
  FROM dates_prices p2
  JOIN reservations r
  ON  (
     r.id = p2.id 
     and r.startdate <= p2.date 
     and p2.date <= r.enddate
  )
)
像极了他 2024-12-29 16:06:55

我已更新查询以排除日期。

select dp.id, dp.date, dp.price
  from dates_prices dp
   left join 
     (select dp.id, dp.date
       from dates_prices dp
       join reservations res 
        on res.id = dp.id 
         and dp.date between res.startdate and res.enddate) as inner_table
     on inner_table.id = dp.id and inner_table.date = dp.date
   where inner_table.id is null 

I've updated the query to exclude dates.

select dp.id, dp.date, dp.price
  from dates_prices dp
   left join 
     (select dp.id, dp.date
       from dates_prices dp
       join reservations res 
        on res.id = dp.id 
         and dp.date between res.startdate and res.enddate) as inner_table
     on inner_table.id = dp.id and inner_table.date = dp.date
   where inner_table.id is null 
苯莒 2024-12-29 16:06:55
create table  table12 (id int, datefor date, price int)

insert  into  table12
select 333, '2011-12-20', 66.00
union all
select 333, '2011-12-21', 66.00
union all
 select 333, '2011-12-22', 66.00
 union all
 select 333, '2011-12-23', 66.00
 union all
select 333, '2011-12-24', 66.00
union all
 select 333, '2011-12-25', 66.00
 union all
 select 333, '2011-12-26', 66.00
 union all
 select 333, '2011-12-27', 66.00
 union all
 select 333, '2011-12-28', 66.00
 union all
 select 333, '2011-12-29', 66.00
 union all
 select 333, '2011-12-30', 66.00 

 create table  table2 (id int, startdate date, enddate date)

insert  into  table2
select 333, '2011-12-20', '2011-12-22'
 union all
 select 333, '2011-12-24', '2011-12-26'
 union all
 select 333, '2011-12-28', '2011-12-30'

创建上述表格后尝试此操作。您的问题的解决方案是您需要对表进行这种类型的查询,即
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。

create proc solution( @id int) 
  as
  begin

declare @count int
    declare @i int
    declare @query varchar(800)
    set @query=''
 declare @startdate varchar(80)
    set @startdate=''
     declare @enddate varchar(80)
    set @enddate=''

 declare @table table( row int ,startdate date,enddate date,id int)
insert into @table select row_number() over(order by (select 1)) as rownumber  ,startdate,enddate,id  from  table2  where table2.id=@id
 set  @count=(select COUNT (*)  from  @table )
 set   @i=1
 set @query += 'select * from  table12 where '
while @count>=@i
begin
 set @startdate=( select startdate  from  @table  where row=@i) 
 set  @enddate =(select enddate  from  @table  where row=@i)
 set @query +=  ' datefor not between '+''''+@startdate+''''+ ' and '+'''' +@enddate+''''
 if   @count>@i
 begin
 set @query +=' and  '
 end
 if @count=@i
 begin
 set @query += ' and  table12.id='+cast (@id as varchar(50)) +''
 end 

set @i+=1
 end
 if(@i=1)
 set @query+='table12.id='+cast (@id as varchar(50)) +''


  exec (@query)
end

exec solution 333
create table  table12 (id int, datefor date, price int)

insert  into  table12
select 333, '2011-12-20', 66.00
union all
select 333, '2011-12-21', 66.00
union all
 select 333, '2011-12-22', 66.00
 union all
 select 333, '2011-12-23', 66.00
 union all
select 333, '2011-12-24', 66.00
union all
 select 333, '2011-12-25', 66.00
 union all
 select 333, '2011-12-26', 66.00
 union all
 select 333, '2011-12-27', 66.00
 union all
 select 333, '2011-12-28', 66.00
 union all
 select 333, '2011-12-29', 66.00
 union all
 select 333, '2011-12-30', 66.00 

 create table  table2 (id int, startdate date, enddate date)

insert  into  table2
select 333, '2011-12-20', '2011-12-22'
 union all
 select 333, '2011-12-24', '2011-12-26'
 union all
 select 333, '2011-12-28', '2011-12-30'

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.

create proc solution( @id int) 
  as
  begin

declare @count int
    declare @i int
    declare @query varchar(800)
    set @query=''
 declare @startdate varchar(80)
    set @startdate=''
     declare @enddate varchar(80)
    set @enddate=''

 declare @table table( row int ,startdate date,enddate date,id int)
insert into @table select row_number() over(order by (select 1)) as rownumber  ,startdate,enddate,id  from  table2  where table2.id=@id
 set  @count=(select COUNT (*)  from  @table )
 set   @i=1
 set @query += 'select * from  table12 where '
while @count>=@i
begin
 set @startdate=( select startdate  from  @table  where row=@i) 
 set  @enddate =(select enddate  from  @table  where row=@i)
 set @query +=  ' datefor not between '+''''+@startdate+''''+ ' and '+'''' +@enddate+''''
 if   @count>@i
 begin
 set @query +=' and  '
 end
 if @count=@i
 begin
 set @query += ' and  table12.id='+cast (@id as varchar(50)) +''
 end 

set @i+=1
 end
 if(@i=1)
 set @query+='table12.id='+cast (@id as varchar(50)) +''


  exec (@query)
end

exec solution 333
蹲在坟头点根烟 2024-12-29 16:06:55

这也应该有效,可能会快得多:

SELECT p1.* FROM dates_prices p1 
WHERE NOT EXISTS
(
  SELECT * FROM reservations r
  WHERE r.id = p1.id 
  AND r.startdate <= p1.date 
  AND p1.date <= r.enddate  
)

This should work too, could be a lot faster:

SELECT p1.* FROM dates_prices p1 
WHERE NOT EXISTS
(
  SELECT * FROM reservations r
  WHERE r.id = p1.id 
  AND r.startdate <= p1.date 
  AND p1.date <= r.enddate  
)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文