-1


在花了 2 天时间试图在网上找到答案后,我对此很陌生,我正在尝试构建一个 MYSQL 选择语句

我在 e 上有 2 个包含日期和价格列表的表,第二个是包含开始和结束的预订日期表。我试图在表 1 中显示所有在表 2 中的任何预订记录中都不存在的免费日期和价格

表 1 称为 - dates_prices -
id, date, price

示例数据

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 中显示的唯一记录如下

身份证、日期、价格

333, 2011-12-23, 66.00
333, 2011-12-27, 66.00
333, 2011-12-31, 66.00
4

4 回答 4

1

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

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 
于 2011-12-21T12:54:00.083 回答
1
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
  )
)
于 2011-12-21T12:55:33.667 回答
0
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'

创建上述表格后尝试此操作。您的问题的解决方案是您需要对表进行此类查询,即从 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 之间,我已经制作了一个 proc 只是你需要传递你的 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
于 2011-12-22T06:28:27.010 回答
0

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

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  
)
于 2011-12-21T14:07:03.603 回答