-1


ネット上で答えを見つけようとして2日を費やした後、私はこれに不慣れです。MYSQLselectステートメントを作成しようとしています。

eには日付と価格のリストを含む2つのテーブルがあり、2つ目は開始日と終了日を含む予約日のテーブルです。表2の予約レコードには存在しない、表1の無料の日付と価格をすべて表示しようとしています。

表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、startdate、enddate

サンプルデータ

333, 2011-12-20, 2011-12-22
333, 2011-12-24, 2011-12-26
333, 2011-12-28, 2011-12-30

同じID番号333を持つ表2のすべてのレコードの開始日と終了日の間に、表2のレコードに存在しない日付のみを表1から抽出する必要があります。

したがって、表1から表示する必要があるレコードは次のとおりです。

ID、日付、価格

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

これも機能するはずですが、はるかに高速になる可能性があります。

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 に答える
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'

上記のテーブルを作成した後、これを試してください。あなたのprobelmの解決策は、テーブルに対してこのタイプのクエリを実行する必要があることです。および「2011-12-26」および「2011-12-28」と「2011-12-30」の間にないdateforおよびtable12.id=333私は1つのprocを作成しました。uridを渡す必要があります。

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 に答える