MYSQL日期时间函数,​日期时间运算,查询1天以内,查询1年以前,查询某个时间区间的记录

MYSQL日期时间函数,日期时间运算,查询1天以内,查询1年以前,查询某个时间区间的记录

日期时间函数

  1、now()  返回服务器当前时间

  2、curdate() 返回当前日期

  3、curtime() 返回当前时间

  4、year(date) 返回指定时间的年份

  5、date(date) 返回指定时间的日期

  6、time(date) 返回指定时间的时间

  7、练习

    1、在表中插入3条记录

      insert into t7 values

      (3,"小昭",19000520,3000,20180630000000),

      (4,"赵敏",19000521,4000,20180702000000),

      (5,"周芷若",19010522,3500,20180702100000);

    2、查找2018年7月2日有哪些用户充值了

   select * from t7 where date(shijian)="2018-07-02";


    3、查找2018年7月份充值的信息

      select * from t7 

      where 

      date(shijian)>="2018-07-01" and date(shijian)<="2018-07-31";


    4、查找7月30日10:00-12:00充值的信息

      select * from t7 

      where

      date(shijian)="2018-07-31" and 

      time(shijian)>="10:00:00" and 

      time(shijian)<="12:00:00";

日期时间运算

  1、语法格式

    select * from 表名

    where 字段名 运算符 (时间-interval 时间间隔单位);

    时间间隔单位:

      1 day | 2 hour | 1 minute | 2 year | 3 month

  2、练习

    1、查询1天以内的记录

      select * from t7 

      where shijian > (now()-interval 1 day);

            age     >   20

    2、查询1年以前的记录

      select * from t7

      where shijian < (now()-interval 1 year);

    3、查询1天以前,3天以内的记录

      select * from t7

      where

      shijian < (now()-interval 1 day) and

      shijian > (now()-interval 3 day);


最后编辑于:2020/10/24作者: 牛逼PHP

发表评论