0

次の表で、既に存在する開始日と終了日の間に開始日と終了日を入力したときに、ユーザーに制限したいと思います。

例: ユーザーは、開始日 2012 年 2 月 6 日と終了日 2012 年 4 月 6 日の日付を挿入しようとします。この日付は、下の表のイベント e2 の間にあるため、ユーザーは挿入できません。

 ------------------------------------------------------------------------
 id               event               startdate    enddate
 ------------------------------------------------------------------------
 11                e1                 31/5/2012    1/6/2012
 12                e2                 1/6/2012     4/6/2012
 13                e3                 5/6/2012     6/6/2012
 14                e4                 15/6/2012    16/6/2012

他のイベントと競合するイベントの数を見つけたいです。

4

1 に答える 1

0
select tbl.startdate, count(tbl.startdate)
from YourTable tbl
where 
exists(select * from YourTable tbl2 where ((tbl2.startdate>=tbl.startdate AND tbl2.startdate<=tbl.enddate) OR (tbl2.enddate>=tbl.startdate AND tbl2.enddate<=tbl.enddate)))
group by tbl.startdate
于 2012-05-31T11:24:47.850 に答える