您的位置:首页 > 数据库 > MySQL

SQL查询月、天、周、年(MySql的实例对比)

2016-06-16 10:37 435 查看
SQL Server实现

日期部分缩写
yearyy, yyyy
quarterqq, q
monthmm, m
dayofyeardy, y
daydd, d
weekwk, ww
weekdaydw
Hourhh
minutemi, n
secondss, s
millisecondms
1 2select datename(weekday,getdate()) 4 5select * from users where year(time)=year(getdate()) 7 8select * from users where month(time)=month(getdate()) and year(time)=year(getdate()) 10 11select * from users where day(time)=day(getdate()) and month(time)=month(getdate()) and year(time)=year(getdate()) 13 14 15SELECT DATEADD(wk, DATEDIFF(wk,0,getdate()), 0) 17 18select dateadd(wk,datediff(wk,0,getdate()),6) 20 21select * from users where DATEPART(wk, time) = DATEPART(wk, GETDATE()) and DATEPART(yy, time) = DATEPART(yy, GETDATE()) 23 24 25select * from users where (DATEDIFF(dd, time, GETDATE()) = 0) 27 28select * from users where (DATEDIFF(mm, time, GETDATE()) = 0) 30 31select * from users where (DATEDIFF(yy, time, GETDATE()) = 0)
在MySql中实现:

1——
2 本年:
3 select * from loanInfo where year(date)=year(getdate())
4
5 2——
6 本月:
7 select * from loanInfo where year(date)=year(getDate()) And month(date)=month(getdate())
8
9 3——
10 本日:
11 select * from loanInfo where year(date)=year(getDate()) And month(date)=month(getdate()) and Day(date)=Day(getDate())
12
13
14
15SELECT * FROM table WHERE (MONTH(字段) = MONTH(GETDATE()))
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: