MYSQL查询规定时间段的每一天
求教:MYSQL查询规定时间段的每一天
如题:比如规定时间是2015年6月28日 至2015年7月5日
那么查询结果应该为
1 2015-6-28
2 2015-6-29
3 2015-6-30
4 2015-7-1
5 2015-7-2
6 2015-7-3
7 2015-7-4
8 2015-7-5
------解决思路----------------------
找了个MYSQL的自动补全日期的, 你看下
http://zhidao.baidu.com/link?url=sG41OMsJInPrcqFWdZe_g3dwGMKm25-lPKS_uk2LzzBTNPJM9870rDWgUpkwRaq_ji1VSmNLwKeM-jAk22-UwAtMPsmHbD9UrNp7G4f4TQG
如题:比如规定时间是2015年6月28日 至2015年7月5日
那么查询结果应该为
1 2015-6-28
2 2015-6-29
3 2015-6-30
4 2015-7-1
5 2015-7-2
6 2015-7-3
7 2015-7-4
8 2015-7-5
------解决思路----------------------
找了个MYSQL的自动补全日期的, 你看下
--创建表
--create table [table](dtime datetime, data varchar(10), num smallint)
--添加测试数据
--insert into [table] select '2002-9-8' ,'data1',0
--insert into [table] select '2002-9-10' ,'data1',3
--insert into [table] select '2002-9-11' ,'data1',4
--测试语句
select * from [table]
--你需要的效果
declare @d table(time datetime)
declare @date datetime
set @date='2002-09-07'
while @date<='2002-09-12'
begin
insert @d select @date
set @date= dateadd(dd,1,cast(@date as datetime))
end
select convert(varchar(10),time,120) as time
, isnull(data, 'data1') as data
,isnull(num, 0) as num
from @d d left join [table] t
on convert(varchar(10),dtime,120) = convert(varchar(10),time,120)
http://zhidao.baidu.com/link?url=sG41OMsJInPrcqFWdZe_g3dwGMKm25-lPKS_uk2LzzBTNPJM9870rDWgUpkwRaq_ji1VSmNLwKeM-jAk22-UwAtMPsmHbD9UrNp7G4f4TQG