我的表包含2个日期字段。像:
id from to
--- ---- --
1 2012-01-01 2012-01-05
2 2012-01-04 2012-01-10
3 2012-01-03 2012-01-07
我想使用日期(例如:2012-01-06)来检查日期是否在表格中的任何日期(从,到)之间。
对于这种情况,我想获得id - 2,3。我怎么能这样做?
答案 0 :(得分:4)
SELECT * FROM table WHERE "2012-01-06" BETWEEN `from` and `to`
或
SELECT * FROM table WHERE `from` <= "2012-01-06" AND "2012-01-06" <= `to`
http://dev.mysql.com/doc/refman/5.5/en/comparison-operators.html
答案 1 :(得分:2)
select id
from myTable
where `from` <= '2012-01-06' and `to` >= '2012-01-06'
答案 2 :(得分:1)
如果列类型为date
,则应用此条件。
WHERE '2012-01-06' BETWEEN `from` AND `to`
答案 3 :(得分:0)
select * from Table where `from` < '2012-01-06' and `to` > '2012-01-06'