0

我有一个产品表和两个 DATETIME 列,一个用于 start_date,一个用于 end_date。

如何检查特定产品 ID 是否介于开始日期和结束日期之间?但是,如果其中一个或两个为 NULL(默认值),则接受此下限/上限为无限制,如果产品仍在另一个范围内(如果已设置)则返回产品。如果两个边界都设置为 NULL,则始终返回产品。

4

2 回答 2

2
select *
from your_table
where
product_id = 1 AND
(
   (CURDATE() between start_date and end_date) 
   or (CURDATE() >= start_date and end_date is null) 
   or (CURDATE() <= end_date and start_date is null)
   or (start_date is null and end_date is null)
)
于 2012-06-27T15:21:53.913 回答
0
select case when ('2012-06-26' between start_date and end_date) 
                or ('2012-06-26' >= start_date and end_date is null) 
                or ('2012-06-26' <= end_date and start_date is null)
            then 1
            else 0
            end as is_between_dates
from your_table
where product_id = 1
于 2012-06-27T15:18:41.857 回答