Mysql 年、月、日 查询

SQL Server实现
日期部分 缩写
year yy, yyyy
quarter qq, q
month mm, m
dayofyear dy, y
day dd, d
week wk, ww
weekday dw
Hour hh
minute mi, n
second ss, s
millisecond ms


1
2/**//*计算今天是星期几*/
3select datename(weekday,getdate())
4
5/**//*查询本年的数据*/
6select * from users where year(time)=year(getdate())  
7
8/**//*查询本月的数据,time是表users中代表时间的字段*/
9select * from users where month(time)=month(getdate()) and year(time)=year(getdate())
10
11/**//*查询今天的数据,time 是表中代表时间的字段*/
12select * from users where day(time)=day(getdate()) and month(time)=month(getdate()) and year(time)=year(getdate())
13
14
15/**//*计算那一天是星期一*/
16SELECT DATEADD(wk, DATEDIFF(wk,0,getdate()), 0)
17
18/**//*计算那一天是周末*/
19select dateadd(wk,datediff(wk,0,getdate()),6)
20
21/**//*查询本周的数据*/
22select * from users where DATEPART(wk, time) = DATEPART(wk, GETDATE()) and DATEPART(yy, time) = DATEPART(yy, GETDATE())
23
24
25/**//*查询本日的记录*/
26select * from users where (DATEDIFF(dd, time, GETDATE()) = 0)
27
28/**//*查询本月的记录*/
29select * from users where (DATEDIFF(mm, time, GETDATE()) = 0)
30
31/**//*查询本年的记录*/
32select * from users where (DATEDIFF(yy, time, GETDATE()) = 0)在MySql中实现:
1 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()))

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值