两天之间的日期

时间:2012-02-08 12:04:00

标签: mysql sql

我想获得给定日期范围内的所有电影ID和名称。例如2012-02-01至2012-02-20应显示ids(1,2和3),它现在显示给定日期范围内的电影。

 movie{ id, name, start_date, end_date, visibility}

id   name  start_date  end_date     visibility
1    abc   2012-01-05   2012-02-10      1
2    xyz   2012-02-05   2012-02-25      1
3    cde   2012-02-19   2012-02-27      1
4    ghi   2012-03-01   2012-03-20      1

5 个答案:

答案 0 :(得分:2)

假设您要查找重叠,假设参数值位于名为params的表中:

SELECT *
  FROM movie AS m, params AS p
 WHERE CASE 
          WHEN m.start_date > p.start_date THEN m.start_date 
          ELSE p.start_date 
       END
       <
       CASE 
          WHEN m.end_date > p.end_date THEN p.end_date 
          ELSE m.end_date 
       END;

答案 1 :(得分:2)

我们有两个区间:

FieldInterval := (start_date, end_date)

TestInterval := (@StartDate, @EndDate),定义为

@StartDate = '2012-02-01' 
@EndDate = '2012-02-20' 

测试FieldInterval是否在TestInterval内:

SELECT id 
FROM movie 
WHERE @StartDate <= start_date      
  AND end_date <= @EndDate 

测试TestInterval是否在FieldInterval内:

SELECT id 
FROM movie 
WHERE start_date <= @StartDate  
  AND @EndDate <= end_date 

测试两个区间是否重叠:

SELECT id 
FROM movie 
WHERE start_date <= @EndDate 
  AND @StartDate <= end_date 

答案 2 :(得分:0)

select id  from movie  
where start_date between '2012-02-01' and '2012-02-20' 
and end_date between '2012-02-01' and '2012-02-20'
and start_date <= end_date;

答案 3 :(得分:0)

我假设您想要将start_date和end_date放入2个参数的电影,对吗?

select id, name from movie
where @startDate < create_date and end_date< @endDate

如果您通知2012-02-01和2012-02-20,它将返回所有在该范围之间有start_date和end_date的电影

答案 4 :(得分:0)

select id, name from `movie` where 
end_date >= '2012-01-05' and 
start_date <= '2012-01-20' 

这对我有用,看起来很简单。