mysql中quarter_MySQL里获取当前week、month、quarter,以及date(),date_format()和str_to_date(),DATEDIFF()操作...
1.查询某年的数据
1.1 select * from oa_item_info where created like '2018-%';
1.2 select * from oa_item_info where left(created,4)='2018';
1.3 select * from oa_item_info where year(created)='2018';
今年的数据:
select * from oa_item_info where year(created)=year(now());
上一年的数据:
select * from oa_item_info where year(created)=year(date_sub(now(),interval 1 year));
date_sub()函数:date_sub
image
image.gif
image
image.gif
2.查询某季度的数据
select QUARTER(created) as quartername ,created from oa_item_info ;
先看一下quarter函数返回的数据,第一列是quartername,第二列是created
1-3月返回1,4-6月返回2,7到9月返回3,10到12月返回4
image
image.gif
并不是搜索本季度的数据:
select * from oa_item_info where QUARTER(created)=QUARTER(now());
这条sql语句返回的是所有年份,当前季度的数据,比如现在是4月,会把所有年份4到6月的数据都检索出来
搜索本季度的数据:
加上本年的限制条件
select * from oa_item_info where QUARTER(created)=QUARTER(now()) and year(created)=year(now());
3.查询某月的数据
select month(created) as monthname,created from oa_item_info;
看一下返回数据:第一列是monthname,第二列是created
image
image.gif
所有年份2月的数据
select * from oa_item_info where month(created)=2;
加上年份的限定条件就可以了
4.查询某周的数据
select week(created) as weekname,created from oa_item_info ;
看一下返回数据:第一列是weekname,第二列是created
返回的是一年中的第几周
image
image.gif
本周的数据:
select created from oa_item_info where week(created)=week(now()) and year(created)=year(now());
select * from oa_item_info where YEARWEEK(date_format(created,'%Y-%m-%d')) = YEARWEEK(now()) ;
看一下week和yearweek的区别:
数据库中加入两条周数一致的日期:
select week('2017-04-20');
image
image.gif
select week('2018-04-25');
image
image.gif
看一下yearweek的返回值:
select yearweek('2018-04-25');
image
image.gif
看一下搜索结果:
select created from oa_item_info where week(created)=week(now());
week把两条年份不一样的都搜出来了
image
image.gif
select created from oa_item_info where YEARWEEK(date_format(created,'%Y-%m-%d')) = YEARWEEK(now()) ;
select created from oa_item_info where YEARWEEK(created) = YEARWEEK(now()) ;
不用date_format函数也可以
yearweek只搜出了今天的
image
image.gif
值得注意的是,他们默认都是从周日开始算的,需要从周一开始计算时,需要加入第二个参数1:week(created,1)
date_format
image
image.gif
上一周的数据:
select * from oa_item_info where YEARWEEK(date_format(created,'%Y-%m-%d')) = YEARWEEK(now())-1;
5.查询某天的数据
今天的数据:
select created from oa_item_info where to_days(created) = to_days(now());
to_days();返回从0年开始的天数;
select to_days(now()) ;
image
image.gif
from_days();根据天数,返回日期;
select from_days(737173) ;
image
image.gif
昨天的数据:
这是很多博文的语句,看一下,现在是24号,会搜出今天和昨天数据
select created from oa_item_info where to_days(now())-to_days(created)<=1 ;
image
image.gif
select created from oa_item_info where to_days(now())-to_days(created)=1 ;
搜出的是昨天的:
image
image.gif
总结:
1.year(),从时间字段获取年
2.quarter(),从时间字段获取季度
3.month(),从时间字段获取月
4.week(),从时间字段获取周
5.yearweek(),从时间字段获取年和周
6.date_sub(), 从时间字段减去指定时间间隔
7.date_format(),时间格式化
8.to_days(),返回从0年开始的天数;
9.from_days(),根据天数,返回日期;
1、DATE() 函数:返回日期或日期时间表达式的日期部分;
2、str_to_date()函数:按照指定日期或时间显示格式 将字符串转换为日期或日期时间类型;
3、date_format()函数:按照指定日期或时间显示格式 输出日期或日期时间;
1、date(datestring)
datestring是合法的日期表达式
如:
SELECT date('2017-02-09 15:25:46.635')
FROM dual;
-->'2017-02-09'
image.gif
2、date_format(datestring,format)
datestring参数是合法的日期。format 规定日期/时间的输出格式。
如:
SELECT STR_TO_DATE('2017-02-09 15:25:46.635','%Y-%m-%d')
FROM dual;
-->'2017-02-09'
SELECT STR_TO_DATE('2017-02-09 15:25:46','%Y-%m-%d %H:%i:%s')
FROM DUAL;
-->'2017-02-09 15:25:46'
SELECT STR_TO_DATE('2017-02-09 15:25','%Y-%m-%d %k:%i')
FROM DUAL;
-->'2017-02-09 15:25:00'
image.gif
3、date_format(datestring,format)
datestring参数是合法的日期。format 规定日期/时间的输出格式。
如:
当前时间按月-日 时:分:秒显示:
SELECT DATE_FORMAT(NOW(),'%m-%d %h:%i %p')
FROM dual;
-->'02-09 06:00 PM'
image.gif
当前时间按 年-月-日 时:分:秒 AM/PM显示:
SELECT DATE_FORMAT(NOW(),'%Y-%m-%d %h:%i:%s %p')
FROM dual;
-->'2017-02-09 06:00:35'
image.gif
当前时间按 年 周 日 时:分:秒显示:
SELECT DATE_FORMAT(NOW(),'%Y %b %d %T')
FROM dual;
-->'2017 Feb 09 18:04:13'
image.gif
可以使用的格式有:
| 格式 | 描述 |
| %a | 缩写星期名 |
| %b | 缩写月名 |
| %c | 月,数值 |
| %D | 带有英文前缀的月中的天 |
| %d | 月的天,数值(00-31) |
| %e | 月的天,数值(0-31) |
| %f | 微秒 |
| %H | 小时 (00-23) |
| %h | 小时 (01-12) |
| %I | 小时 (01-12) |
| %i | 分钟,数值(00-59) |
| %j | 年的天 (001-366) |
| %k | 小时 (0-23) |
| %l | 小时 (1-12) |
| %M | 月名 |
| %m | 月,数值(00-12) |
| %p | AM 或 PM |
| %r | 时间,12-小时(hh:mm:ss AM 或 PM) |
| %S | 秒(00-59) |
| %s | 秒(00-59) |
| %T | 时间, 24-小时 (hh:mm:ss) |
| %U | 周 (00-53) 星期日是一周的第一天 |
| %u | 周 (00-53) 星期一是一周的第一天 |
| %V | 周 (01-53) 星期日是一周的第一天,与 %X 使用 |
| %v | 周 (01-53) 星期一是一周的第一天,与 %x 使用 |
| %W | 星期名 |
| %w | 周的天 (0=星期日, 6=星期六) |
| %X | 年,其中的星期日是周的第一天,4 位,与 %V 使用 |
| %x | 年,其中的星期一是周的第一天,4 位,与 %v 使用 |
| %Y | 年,4 位 |
| %y | 年,2 位 |
4.DATEDIFF() 函数
DATEDIFF() 函数返回两个日期之间的时间。
语法
DATEDIFF(datepart,startdate,enddate)
startdate 和 enddate 参数是合法的日期表达式。
datepart 参数可以是下列的值:
| datepart | 缩写 |
| 年 | yy, yyyy |
| 季度 | qq, q |
| 月 | mm, m |
| 年中的日 | dy, y |
| 日 | dd, d |
| 周 | wk, ww |
| 星期 | dw, w |
| 小时 | hh |
| 分钟 | mi, n |
| 秒 | ss, s |
| 毫秒 | ms |
| 微妙 | mcs |
| 纳秒 | ns |
例子 1
使用如下 SELECT 语句:
SELECT DATEDIFF(day,'2008-12-29','2008-12-30') AS DiffDate
结果:
| DiffDate |
| 1 |
例子 2
使用如下 SELECT 语句:
SELECT DATEDIFF(day,'2008-12-30','2008-12-29') AS DiffDate
结果:
| DiffDate |
| -1 |
参考:
mysql中quarter_MySQL里获取当前week、month、quarter,以及date(),date_format()和str_to_date(),DATEDIFF()操作...相关推荐
- mysql查询单词出现的位置_在MySQL中从左侧获取一些单词
为此,请在MySQL中使用LEFT.让我们首先创建一个表-mysql> create table DemoTable -> ( -> Title text -> ); 使用插入 ...
- mysql where 位子,mysql中 地里位置搜索 (抄)
mysql中geometry类型的简单使用 ozil_oo关注 0.1892018.08.09 10:31:48字数 304阅读 4,012 创建表格脚本 这里我创建了一张位置信息表,每个人对应的经纬 ...
- mysql中quarter_MySQL QUARTER()用法及代码示例
MySQL中的QUARTER()函数用于返回给定日期值的一年的季度.它返回一个从1到4的数字. 用法: QUARTER(date) 参数:该函数仅接受一个参数 date:我们要从中提取季度的日期或Da ...
- mysql去掉两个最高分_从MySQL中的单列获取最高得分值,从两列获取最高得分值...
让我们首先创建一个表-mysql> create table DemoTable790 ( Score1 int, Score2 int ); 使用插入命令在表中插入一些记录-mysql> ...
- 向mysql中添加更新时间_mysql 实现添加时间自动添加更新时间自动更新操作
在数据库使用中经常使用到时间字段.常用的有创建时间和更新时间. 然而在使用中想要创建时间在创建的时候自动设置为当前时间,更新时间在更新时自动更新为当前时间. 创建表 stu CREATE TABLE ...
- mysql 获取距离当前最新的记录_一文带你了解 MySQL 中的各种锁机制!
云栖号资讯:[点击查看更多行业资讯] 在这里您可以找到不同行业的第一手的上云资讯,还在等什么,快来! MySQL中的锁机制,按粒度分为行级锁,页级锁,表级锁,其中按用法还分为共享锁和排他锁. 行级锁 ...
- Mysql中获取行号
mysql中是没有获取行号的函数的,但是我们可以通过定义变量来获取行号下面就举个简单的例子. 表中数据如下所示: 接下来对价格'price'进行排序标记行号,sql如下: SELECT @rown ...
- MySQL中的时间类型
时间是一类重要的数据,MySQL中有多种关于时间的类型可以选择.这篇文章主要介绍MySQL中的时间类型,主要参考MySQL文档:https://dev.mysql.com/doc/refman/8.0 ...
- 一文带你了解 MySQL 中的各种锁机制!
MySQL中的锁机制,按粒度分为行级锁,页级锁,表级锁,其中按用法还分为共享锁和排他锁. 行级锁 行级锁是Mysql中锁定粒度最细的一种锁,表示只针对当前操作的行进行加锁. 行级锁能大大减少数据库操作 ...
最新文章
- linux无后缀名程序运行,linux – 如何在Ubuntu上运行无扩展(也许是ELF)文件?
- linux消息总线日志,linux – 解密继续mpt2sas系统日志消息
- 计算机积木游戏,乐高积木模拟器
- Java中事件驱动程序设计_【JAVA语言程序设计基础篇】--事件驱动程序设计--鼠标事件...
- jQery 操作CSS
- 剑指offer最新版_剑指Offer——Java版本(持续更新)
- 巴洛克式和哥特式的区别
- 添加源代码到你的sdk中
- Centos硬盘IO性能检测命令iostat[转]
- 全国植被覆盖度VFC逐月数据
- delphi2007、2010无法二次启动,报错 EditorLineEnds.ttr 另一个程序正在使用此文件
- Java算法面试题(009) 毒酒问题
- window10运行不了1stopt_1stOpt批量处理方程
- 1055 集体照 (25 分)
- 多线程+socket 实现群聊服务器
- Ubuntu22.04更换国内镜像源(阿里、网易163、清华、中科大)
- Python3 《零基础小白从入门到实战》之“continue 语句”
- phonegap mysql_phoneGap-Android开发环境搭建
- discuz二次开发技巧
- Windows 11 插上U盘之后鼠标移动非常卡