mysql关于日期的一些常见使用
前言
1、date_format
多个条件分组,通过传入的日期
,以及自身的tpe,activityId进行分组
date_format(passTime,'%Y-%m-%d %H:%i:%S')
1.1、条件分组
SELECT date_format(passTime,'%Y-%m-%d'),type ,activityId, sum(ifnull(settleMoney,0)) as settleMoney ,sum(ifnull(award,0)) award FROM cpl_step_passed_record
where date_format(passTime,'%Y-%m-%d') = '2018-02-27'
GROUP BY type,activityId,date_format(passTime,'%Y-%m-%d');
1.2、查询匹配,注意传入的是字符串
void deleteByDate(@Param("dayDate") String dayDate);
~~~~
<delete id="deleteByDate" >
delete c.* FROM coupon_check_valid c WHERE date_format(c.cdate,'%Y-%m-%d' ) = #{dayDate}
</delete>
1.3、测试
create table date_test(
id bigint(20) not null auto_increment ,
name varchar(20) default '',
yyyyMMdd date default null ,
yyyyMMddHHmmss datetime default null ,
primary key (id)
)
INSERT INTO date_test (id, name, yyyyMMdd, yyyyMMddHHmmss) VALUES (1, 'healerjean', '2018-12-12', '2018-12-12 23:11:11');
# yyyyMMddHHmmss 存储数据为 2018-12-12 23:11:11
select * from date_test d where date_format(d.yyyyMMddHHmmss,"%Y-%m-%d") = '2018-12-12';
select * from date_test d where date_format(d.yyyyMMddHHmmss,"%Y-%m-%d %H:%i:%s") = '2018-12-12 23:11:11';
# yyyyMMdd 存储的为 2018-12-12
select * from date_test d where date_format(d.yyyyMMdd,"%Y-%m-%d") = '2018-12-12';
select * from date_test d where date_format(d.yyyyMMdd, "%Y-%m-%d %H:%i:%s") = '2018-12-12 00:00:00';
select * from date_test d where d.yyyyMMdd = STR_TO_DATE('2018-12-12','%Y-%m-%d') ;
select * from date_test d where d.yyyyMMdd = STR_TO_DATE('2018-12-12 00:00:00','%Y-%m-%d %H:%i:%s') ;
# 下面这个找不到数据,说明date类型默认的时分秒 为00:00:00
select * from date_test d where date_format(d.yyyyMMdd, "%Y-%m-%d %H:%i:%s") = '2018-12-12 11:00:00';
2、to_char()
/**
* 首页新闻展示查询
* @param s
* @return
*/
@Query(value = "select new cn.edu.dlut.career.dto.school.NewsDTO(n.id,n.title,n.publishDate) from News n where n.newsColumn = ?1 AND (n.isTop = FALSE OR (n.isTop = TRUE AND n.topEndDate <= to_char(now(), 'YYYY-MM-DD')))\n" +
"AND n.endDate>= to_char(now(), 'YYYY-MM-DD') order by n.publishDate DESC")
LinkedList<NewsDTO> findByNewsColumn(String s, Pageable pageable);
3、to_date()、to_timestamp() 这两种是在oracle和postgresql中的,mysql中没有,但是提供了类似的
date类型是Oracle常用的日期型变量,他的时间间隔是秒。两个日期型相减得到是两个时间的间隔,注意单位是“天”。例如:查看一下当前距离伦敦奥运会开幕还有多长时间:
1 select to_date('2012-7-28 03:12:00','yyyy-mm-dd hh24:mi:ss')-sysdate from dual
结果是:92.2472685185185天,然后你根据相应的时间换算你想要的间隔就行!这个结果可能对程序员有用,对于想直接看到结果的人,这个数字还不是很直观,所以,就引出了timestamp类型
timestamp是DATE类型的扩展,可以精确到小数秒(fractional_seconds_precision),可以是0 to9,缺省是6。两个timestamp相减的话,不能直接的得到天数书,而是得到,
多少天,多少小时,多少秒等,例如:同样查看一下当前距离伦敦奥运会开幕还有多长时间.
1 select to_timestamp('2012-7-28 03:12:00','yyyy-mm-dd hh24:mi:ss')-systimestamp from dual
结果是:+000000092 05:51:24.032000000,稍加截取,就可以得到92天5小时,51分钟,24秒,这样用户看起来比较直观一些!但是这个数字对程序员来说不是很直观了,如果想要具体的时间长度的话,并且精度不要求到毫秒的话,
4、timestampdiff() 选择大于或小于某个时间段的数据
获取48小时之内的数据
<select id="find48HoursDistinctOpenId" resultType="com.duodian.youhui.entity.db.callperson.OnlineChatPerson">
<![CDATA[ SELECT * from call_online_chat_person c WHERE TIMESTAMPDIFF(HOUR,c.cdate,now()) < 48 ]]>
</select>
<!--
//前面减去后面,前面为过去事件
select TIMESTAMPDIFF(HOUR,'2018-05-30 00:00:00',now()) as '时间差' ;
SECOND 秒 SECONDS
MINUTE 分钟 MINUTES
HOUR 时间 HOURS
DAY 天 DAYS
MONTH 月 MONTHS
YEAR
-->
5、unix_timestamp()获取日期的时间戳
SELECT unix_timestamp(u.date) from user_info u;
1528427765000 毫秒
6、from_unixtime()时间戳转化为日期(时间戳为毫秒)
SELECT from_unixtime(1500109248, '%Y-%m-%d %H:%i:%S');
2017-07-15 17:00:48
7、查询一些特定日期
今天
select * from 表名 where to_days(时间字段名) = to_days(now());
昨天
SELECT * FROM 表名 WHERE TO_DAYS( NOW( ) ) - TO_DAYS( 时间字段名) <= 1
7天
SELECT * FROM 表名 where DATE_SUB(CURDATE(), INTERVAL 7 DAY) <= date(时间字段名)
近30天
SELECT * FROM 表名 where DATE_SUB(CURDATE(), INTERVAL 30 DAY) <= date(时间字段名)
本月
SELECT * FROM 表名 WHERE DATE_FORMAT( 时间字段名, '%Y%m' ) = DATE_FORMAT( CURDATE( ) , '%Y%m' )
上一月
SELECT * FROM 表名 WHERE PERIOD_DIFF( date_format( now( ) , '%Y%m' ) , date_format( 时间字段名, '%Y%m' ) ) =1
#查询本季度数据
select * from `ht_invoice_information` where QUARTER(create_date)=QUARTER(now());
#查询上季度数据
select * from `ht_invoice_information` where QUARTER(create_date)=QUARTER(DATE_SUB(now(),interval 1 QUARTER));
#查询本年数据
select * from `ht_invoice_information` where YEAR(create_date)=YEAR(NOW());
#查询上年数据
select * from `ht_invoice_information` where year(create_date)=year(date_sub(now(),interval 1 year));
查询当前这周的数据
SELECT name,submittime FROM enterprise WHERE YEARWEEK(date_format(submittime,'%Y-%m-%d')) = YEARWEEK(now());
查询上周的数据
SELECT name,submittime FROM enterprise WHERE YEARWEEK(date_format(submittime,'%Y-%m-%d')) = YEARWEEK(now())-1;
查询当前月份的数据
select name,submittime from enterprise where date_format(submittime,'%Y-%m')=date_format(now(),'%Y-%m')
查询距离当前现在6个月的数据
select name,submittime from enterprise where submittime between date_sub(now(),interval 6 month) and now();
8、cast 将其他类型的字段转化为数据库date 进行判断
localdate
"(s.endDate >= ?9 or cast(?9 as date)=null) and " +
"(s.endDate <= ?10 or cast(?10 as date)=null) and " +
LocalDateTime timestamp
如果数据库字段是String类型的,传入的类似是 LocalDate
Query(value = "from News as n where (" +
"((cast(n.endDate as LocalDate) <= ?3 ) or cast(?3 as LocalDate) =null) and " +
Page<News> listPageNews(String title, LocalDate publishDate, LocalDate endDate, LocalDate sortEndDate, String newsColumn, Pageable pageable);
9、摘自网络
https://www.cnblogs.com/ggjucheng/p/3352280.html
9.1、Date/Time to Str(日期/时间转换为字符串)函数:date_format(date,format)
, time_format(time,format)
mysql> select date_format('2008-08-08 22:23:01', '%Y%m%d%H%i%s');
+----------------------------------------------------+
| date_format('2008-08-08 22:23:01', '%Y%m%d%H%i%s') |
+----------------------------------------------------+
| 20080808222301 |
+----------------------------------------------------+
9.2、Str to Date (字符串转换为日期)函数:str_to_date(str, format)
select str_to_date('08/09/2008', '%m/%d/%Y'); -- 2008-08-09
select str_to_date('08/09/08' , '%m/%d/%y'); -- 2008-08-09
select str_to_date('08.09.2008', '%m.%d.%Y'); -- 2008-08-09
select str_to_date('08:09:30', '%h:%i:%s'); -- 08:09:30
select str_to_date('08.09.2008 08:09:30', '%m.%d.%Y %h:%i:%s'); -- 2008-08-09 08:09:30
9.3、(日期、天数)转换函数:to_days(date), from_days(days)
select to_days('0000-00-00'); -- 0
select to_days('2008-08-08'); -- 733627
9.4、(时间、秒)转换函数:time_to_sec(time), sec_to_time(seconds)__
select time_to_sec('01:00:05'); -- 3605
select sec_to_time(3605); -- '01:00:05'
9.5、拼凑日期、时间函数:makdedate(year,dayofyear), maketime(hour,minute,second)
select makedate(2001,31); -- '2001-01-31'
select makedate(2001,32); -- '2001-02-01'
select maketime(12,15,30); -- '12:15:30'
9.6、Unix 时间戳、日期)转换函数
unix_timestamp(),
unix_timestamp(date),
from_unixtime(unix_timestamp),
from_unixtime(unix_timestamp,format)
select unix_timestamp(); -- 1218290027
select unix_timestamp('2008-08-08'); -- 1218124800
select unix_timestamp('2008-08-08 12:30:00'); -- 1218169800
select from_unixtime(1218290027); -- '2008-08-09 21:53:47'
select from_unixtime(1218124800); -- '2008-08-08 00:00:00'
select from_unixtime(1218169800); -- '2008-08-08 12:30:00'
select from_unixtime(1218169800, '%Y %D %M %h:%i:%s %x'); -- '2008 8th August 12:30:00 2008'
如果满意,请打赏博主任意金额,感兴趣的在微信转账的时候,添加博主微信哦, 请下方留言吧。可与博主自由讨论哦
支付包 | 微信 | 微信公众号 |
---|---|---|
![]() |
![]() |
![]() |