如何在MySQL中获取两个日期之间的日期列表选择查询我希望通过选择查询将日期列表放在两个日期之间。例如:如果我给“2012-02-10”和“2012-02-15”,我需要结果。date
----------
2012-02-10
2012-02-11
2012-02-12
2012-02-13
2012-02-14
2012-02-15我怎么能得到?
3 回答
潇潇雨雨
TA贡献1833条经验 获得超4个赞
select * from (select adddate('1970-01-01',t4.i*10000 + t3.i*1000 + t2.i*100 + t1.i*10 + t0.i) selected_date from (select 0 i union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t0, (select 0 i union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t1, (select 0 i union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t2, (select 0 i union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t3, (select 0 i union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t4) vwhere selected_date between '2012-02-10' and '2012-02-15'
POPMUISE
TA贡献1765条经验 获得超5个赞
set @i = -1;SELECT DATE(ADDDATE('2012-02-10', INTERVAL @i:=@i+1 DAY)) AS date FROM `table`HAVING @i < DATEDIFF('2012-02-15', '2012-02-10')
呼如林
TA贡献1798条经验 获得超3个赞
您可以创建一个表,其中包含您可能需要使用的所有日期:
date
2000-01-01
2000-01-02
2000-01-03
...etc..
2100-12-30
2100-12-31
然后按以下方式查询该表:
SELECT date
FROM dates
WHERE date BETWEEN '2012-02-10' AND '2012-02-15'
添加回答
举报
0/150
提交
取消