================================================數據庫
一、SQL查詢一年以內的數據記錄函數
select * from 表名 where CreateDate<GETDATE() and CreateDate>DATEADD(yy, -1, GETDATE())
二、--查詢當天記錄:spa
select * from info where DateDiff(dd,datetime,getdate())=0
三、--查詢24小時內的記錄:
code
select * from info where DateDiff(hh,datetime,getDate())<=24
五、--查詢本週記錄blog
select * from info where datediff(week,datetime,getdate())=0
六、--查詢本月記錄
get
select * from info where datediff(month,datetime,getdate())=0
--info爲表名,datetime爲數據庫中的日期字段it
DATEDIFF 函數:class
語法:date
DATEDIFF ( datepart , startdate , enddate )select
七、統計每一年
select year(ordertime) AS '年', sum(Total) '銷售合計' from order_list group by year(ordertime)
八、統計每個月
select year(ordertime) '年', month(ordertime) '月', sum(Total) '銷售合計' from order_list group by year(ordertime), month(ordertime)
九、統計每日
select year(ordertime) '年', month(ordertime) '月', day(ordertime) '日', sum(Total) '銷售合計' from order_list group by year(ordertime), month(ordertime), day(ordertime)
另外也能夠這樣:
select convert(char(8),ordertime,112) dt, sum(Total) '銷售合計' from order_list group by convert(char(8),ordertime,112)
十、每個月(年、日)的記錄條數
select year(ordertime) '年', month(ordertime) '月', count(*) '銷售記錄' from order_list group by year(ordertime), month(ordertime)
十一、
十二、
1三、
1四、
1五、
1六、
=================================================