My sql 如何用一条语句生成整月日期并按日期排序,跪求高人

如题所述

第1个回答  2012-01-14
declare @sdate datetime ,@edate datetime
set @sdate = '2012/01/01 00:00:00'
set @edate = '2012/01/31 00:00:00 '

while @sdate < = @edate
begin
print @sdate
dateadd(day,1,@sdate)
end

这个只是显示出结果,如果要插入某个表格的话,
将print那行该一下应该就可以了
第2个回答  2012-01-14
获取当月的
select (curdate()-interval day(curdate()) day) + interval s day as dt
from (
select 1 as s
union all
select 2
union all
select 3
union all
select 4
union all
select 5
union all
select 6
union all
select 7
union all
select 8
union all
select 9
union all
select 10
union all
select 11
union all
select 12
union all
select 13
union all
select 14
union all
select 15
union all
select 16
union all
select 17
union all
select 18
union all
select 19
union all
select 20
union all
select 21
union all
select 22
union all
select 23
union all
select 24
union all
select 25
union all
select 26
union all
select 27
union all
select 28
union all
select 29
union all
select 30
union all
select 31
) m
where s<=day(LAST_DAY(CURDATE()));
获取指定月的只要把所有CURDATE()改成你的月份,比如'2012-02-01'
select ('2012-02-01'-interval day('2012-02-01') day) + interval s day as dt
from (
select 1 as s
union all
select 2
union all
select 3
union all
select 4
union all
select 5
union all
select 6
union all
select 7
union all
select 8
union all
select 9
union all
select 10
union all
select 11
union all
select 12
union all
select 13
union all
select 14
union all
select 15
union all
select 16
union all
select 17
union all
select 18
union all
select 19
union all
select 20
union all
select 21
union all
select 22
union all
select 23
union all
select 24
union all
select 25
union all
select 26
union all
select 27
union all
select 28
union all
select 29
union all
select 30
union all
select 31
) m
where s<=day(LAST_DAY('2012-02-01'));本回答被提问者采纳
第3个回答  2012-01-14
declare @sdate datetime
declare @edate datetime
set @sdate = '2012-01-01'
set @edate = '2012-01-31'

select
日期 = dateadd(dd,num,@sdate)
from
(select isnull((select count(1) from sysobjects where id<t.id),0) as num from sysobjects t) a
where
dateadd(dd,num,@sdate)<=@edate
order by 日期追问

这段代码不能在my SQL窗口中运行,提示从第一行就错了,另外系统提示 sysobjects 找不到,我了解my sql较少,不知道是那个表,请高手告知一下
谢了

追答

sysobjects是记录数据库中所有表的信息

本回答被网友采纳
相似回答