SQL查询月、天、周、年(MySql的实例对比)
2016-06-16 10:37
435 查看
SQL Server实现
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()))
日期部分 | 缩写 |
---|---|
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 |
在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()))
相关文章推荐
- MySQL优化实例
- MYSQL(一)
- Mysql命令大全
- mysql导出数据库表结构以及数据
- 【Mysql】关于任意日期的比较
- MySQL中表分区技术详细解析
- 欢迎使用CSDN-markdown编辑器
- MySQL性能优化经验
- Mysql转存SQL后,重新运行SQL,第一行正确,第二行开始报乱码
- MySQL设计规约
- MYSQL的常用命令和增删改查语句和数据类型
- MySQL日期数据类型、时间类型总结
- MySQL 索引原理及慢查询优化
- Cobar使用文档(可用作MySQL大型集群解决方案)
- 更改MAMP的mysql密码
- 欢迎使用CSDN-markdown编辑器
- MySQL存储过程详解 mysql 存储过程
- MySQL.zip安装及初始化
- MySQL删除存储过程和函数
- MySQL存储过程详解 mysql 存储过程