zoukankan      html  css  js  c++  java
  • mysql 时间范围

    - 查询今天的数据

    select * from `user` where to_days(birthday) = to_days(CURDATE());

    -- 查询昨天的数据

    select * from `user` where to_days(CURDATE()) - to_days(birthday)<=1;

    -- 查询最近7天的数据

    select * from `user` where birthday > DATE_SUB(CURDATE(),INTERVAL 7 DAY);

    -- 查询最近一个季度的数据

    select * from `user` where birthday > DATE_SUB(CURDATE(), INTERVAL 3 MONTH)

    -- 最近一年的数据

    select * from `user` where birthday > DATE_SUB(CURDATE(), INTERVAL 1 YEAR);

     

    -- 本季度的数据

    select * from `user` where quarter(birthday) = quarter(CURDATE());

    -- 上季度的数据

    select * from `user` where quarter(birthday) = quarter(DATE_SUB(CURDATE(), INTERVAL 1 QUARTER));

     

    -- 查询今年的数据

    select * from `user` where year(CURDATE()) - year(birthday) = 28 ;

     

    -- 查询第几月的数据

    select * from `user` where month(birthday) = 8 ;

     

    -- 查询某年某月某日的数据

    select * from `user` where date_format(birthday,'%Y-%m-%d')='2017-07-07';

     

    -- 查询制定时间段内的数据(只写到月,会出错)

    select * from `user` where birthday between '1888-5-1 00:00:00' and '2017-9-3 00:00:00';

     

    -- 查询制定时间段内的数据(只写到月,会出错)

    select * from `user` where birthday > '1989-5-1' and birthday < '2017-5-1';

    mysql日期与字符串转换函数

    字符串转日期:STR_TO_DATE('2019-07-25 00:00:33', '%Y-%m-%d %H:%i:%s')  ,走索引

    日期转字符串:DATE_FORMAT('2019-07-25 00:00:33', '%Y-%m-%d %H:%i:%s'),不走索引

    方式一、between...and(推荐)

    SELECT * FROM k_student WHERE create_time  between '2019-07-25 00:00:33' and '2019-07-25 00:54:33'

    方式二、大小于号

    SELECT * FROM k_student WHERE create_time >= '2019-07-25 00:00:33' AND create_time <= '2019-07-25 00:54:32'

    方式三、转换为UNIX_TIMESTAMP比较,create_time若加了索引,不走索引 

    SELECT * FROM k_student WHERE  UNIX_TIMESTAMP(create_time)  between UNIX_TIMESTAMP('2019-07-25 00:00:33') and UNIX_TIMESTAMP('2019-07-25 00:54:33')

  • 相关阅读:
    spring cloud alibaba +seata 实战中Error processing condition on io.seata.spring.boot.autoconfigure.问题总结
    Docker部署Elasticsearch及安装后自动关闭的问题
    SpringBoot10:Web开发静态资源处理
    SpringBoot09:整合MyBatis
    SpringBoot08:整合Druid
    SpringBoot07:整合JDBC
    SpringBoot06:自定义starter
    SpringBoot05:自动配置原理
    SpringBoot04:JSR303数据校验及多环境切换
    SpringBoot03:yaml配置注入
  • 原文地址:https://www.cnblogs.com/endv/p/12774409.html
Copyright © 2011-2022 走看看