如何查询某些日期范围的数据库

时间:2014-02-17 07:20:19

标签: php mysql sql

我需要查询mysql以获取某个范围的详细信息。说,我的范围来自

'2/1/2013' to '8/1/2013',即7天。

要获得此范围,我可以在mysql查询中使用“from”,“to”。 但是,我应该如何使用查询来获得相同范围的特定数量的结果。

如果我需要获得接下来的3组结果,那么Query应该将结果带入范围内的3列结果中,

'2/1/2013' to '8/1/2013'
'9/1/2013' to '15/1/2013'
'16/1/2013' to '22/1/2013'`. 

由于在相同条件下我需要3行3个结果,这可以通过使用单个查询吗?

4 个答案:

答案 0 :(得分:1)

Use This Link

test_expression [ NOT ] BETWEEN begin_expression AND end_expression

答案 1 :(得分:0)

为什么不使用

中的Between关键字
select * from table where TO >= '18/03/2011' AND TO <= '18/09/2011'
union
select * from table where TO >= '18/01/2011' AND TO <= '18/02/2011'
union
select * from table where FROM >= '18/03/2011' AND FROM <= '18/09/2011'
union
select * from table where FROM >= '18/01/2011' AND FROM <= '18/02/2011'

答案 2 :(得分:0)

使用此查询并替换您的表名

SELECT * FROM table WHERE from >= '2013-01-02' AND to < '2011-01-08' 
UNION SELECT * FROM table WHERE from >= '2013-01-09' AND to < '2011-01-15' 
UNION SELECT * FROM table WHERE from >= '2013-01-16' AND to < '2011-01-22';

答案 3 :(得分:0)

是的,可以在单个查询中获得您的预期结果: -

select * from tableName where ((dt between '2/1/2013' AND '8/1/2013') 
    or (dt between '9/1/2013' AND '15/1/2013') 
    or (dt between '16/1/2013' AND '22/1/2013'));