zoukankan      html  css  js  c++  java
  • MySQL时间差返回月个数

    Sql代码  收藏代码
    1. select PERIOD_DIFF(date_format(now(),'%Y%m'),date_format('2010-11-30','%Y%m'))   


    1. MySQL 为日期增加一个时间间隔:date_add() 

    Sql代码  收藏代码
    1. set @dt = now();  
    2. select date_add(@dt, interval 1 day); -- add 1 day  
    3. select date_add(@dt, interval 1 hour); -- add 1 hour  
    4. select date_add(@dt, interval 1 minute); -- ...  
    5. select date_add(@dt, interval 1 second);  
    6. select date_add(@dt, interval 1 microsecond);  
    7. select date_add(@dt, interval 1 week);  
    8. select date_add(@dt, interval 1 month);  
    9. select date_add(@dt, interval 1 quarter);  
    10. select date_add(@dt, interval 1 year);  
    11. select date_add(@dt, interval -1 day); -- sub 1 day  


    MySQL adddate(), addtime()函数,可以用 date_add() 来替代。下面是 date_add() 实现 addtime() 功能示例: 

    Sql代码  收藏代码
    1. mysql> set @dt = '2008-08-09 12:12:33';  
    2. mysql>  
    3. mysql> select date_add(@dt, interval '01:15:30' hour_second);  
    4. +------------------------------------------------+  
    5. | date_add(@dt, interval '01:15:30' hour_second) |  
    6. +------------------------------------------------+  
    7. | 2008-08-09 13:28:03 |  
    8. +------------------------------------------------+  
    9. mysql> select date_add(@dt, interval '1 01:15:30' day_second);  
    10. +-------------------------------------------------+  
    11. | date_add(@dt, interval '1 01:15:30' day_second) |  
    12. +-------------------------------------------------+  
    13. | 2008-08-10 13:28:03 |  
    14. +-------------------------------------------------+  


    date_add() 函数,分别为 @dt 增加了“1小时 15分 30秒” 和 “1天 1小时 15分 30秒”。建议:总是使用 date_add() 日期时间函数来替代 adddate(), addtime()。 

      2. MySQL 为日期减去一个时间间隔:date_sub() 

    Sql代码  收藏代码
    1. mysql> select date_sub('1998-01-01 00:00:00', interval '1 1:1:1' day_second);  
    2.   
    3. +----------------------------------------------------------------+  
    4. | date_sub('1998-01-01 00:00:00', interval '1 1:1:1' day_second) |  
    5. +----------------------------------------------------------------+  
    6. | 1997-12-30 22:58:59 |  
    7. +----------------------------------------------------------------+  


    MySQL date_sub() 日期时间函数 和 date_add() 用法一致,不再赘述。另外,MySQL 中还有两个函数 subdate(), subtime(),建议,用 date_sub() 来替代。 

      3. MySQL 另类日期函数:period_add(P,N), period_diff(P1,P2) 
      函数参数“P” 的格式为“YYYYMM” 或者 “YYMM”,第二个参数“N” 表示增加或减去 N month(月)。 
    MySQL period_add(P,N):日期加/减去N月。 

    Sql代码  收藏代码
    1. mysql> select period_add(200808,2), period_add(20080808,-2)  
    2. +----------------------+-------------------------+  
    3. | period_add(200808,2) | period_add(20080808,-2) |  
    4. +----------------------+-------------------------+  
    5. | 200810 | 20080806 |  
    6. +----------------------+-------------------------+  


    MySQL period_diff(P1,P2):日期 P1-P2,返回 N 个月。 

    Sql代码  收藏代码
    1. mysql> select period_diff(200808, 200801);  
    2. +-----------------------------+  
    3. | period_diff(200808, 200801) |  
    4. +-----------------------------+  
    5. | 7 |  
    6. +-----------------------------+  


    在 MySQL 中,这两个日期函数,一般情况下很少用到。 

    4. MySQL 日期、时间相减函数:datediff(date1,date2), timediff(time1,time2) 
    MySQL datediff(date1,date2):两个日期相减 date1 - date2,返回天数。 

    Sql代码  收藏代码
    1. select datediff('2008-08-08', '2008-08-01'); -- 7  
    2. select datediff('2008-08-01', '2008-08-08'); -- -7  


    MySQL timediff(time1,time2):两个日期相减 time1 - time2,返回 time 差值。 

    Sql代码  收藏代码
    1. select timediff('2008-08-08 08:08:08', '2008-08-08 00:00:00'); -- 08:08:08  
    2. select timediff('08:08:08', '00:00:00'); -- 08:08:08  


    注意:timediff(time1,time2) 函数的两个参数类型必须相同。 

    MySQL查询的方式很多,下面为您介绍的MySQL查询实现的是查询本周、上周、本月、上个月份的数据,如果您对MySQL查询方面感兴趣的话,不妨一看。 

    查询当前这周的数据 

    Sql代码  收藏代码
    1. SELECT name,submittime FROM enterprise WHERE YEARWEEK(date_format(submittime,'%Y-%m-%d')) = YEARWEEK(now());  


    查询上周的数据 

    Sql代码  收藏代码
    1. SELECT name,submittime FROM enterprise WHERE YEARWEEK(date_format(submittime,'%Y-%m-%d')) = YEARWEEK(now())-1;  


    查询当前月份的数据 

    Sql代码  收藏代码
    1. select name,submittime from enterprise   where date_format(submittime,'%Y-%m')=date_format(now(),'%Y-%m')  



    查询距离当前现在6个月的数据 

    Sql代码  收藏代码
    1. select name,submittime from enterprise where submittime between date_sub(now(),interval 6 month) and now();  



    查询上个月的数据 

    Sql代码  收藏代码
    1. select name,submittime from enterprise   where date_format(submittime,'%Y-%m')=date_format(DATE_SUB(curdate(), INTERVAL 1 MONTH),'%Y-%m')  



    Sql代码  收藏代码
      1. select * from `user` where DATE_FORMAT(pudate,'%Y%m') = DATE_FORMAT(CURDATE(),'%Y%m') ;  
      2.   
      3. select * from user where WEEKOFYEAR(FROM_UNIXTIME(pudate,'%y-%m-%d')) = WEEKOFYEAR(now())  
      4.   
      5. select *  
      6. from user  
      7. where MONTH(FROM_UNIXTIME(pudate,'%y-%m-%d')) = MONTH(now())  
      8.   
      9. select *  
      10. from [user]  
      11. where YEAR(FROM_UNIXTIME(pudate,'%y-%m-%d')) = YEAR(now())  
      12. and MONTH(FROM_UNIXTIME(pudate,'%y-%m-%d')) = MONTH(now())  
      13.   
      14. select *  
      15. from [user]  
      16. where pudate between 上月最后一天  
      17. and 下月第一天  
  • 相关阅读:
    【好书摘要】性能优化中CPU、内存、磁盘IO、网络性能的依赖
    【转】性能调优从哪里入手
    【转】从来没有冲突的团队是最糟糕的团队
    【转】华为Java编程军规,每季度代码验收标准
    【转】性能测试随笔,看看罢了,只做笑谈尔。
    MVC的JsonResult用法
    artDialog
    js apply/call/caller/callee/bind使用方法与区别分析
    jquery的each()详细介绍
    ASP.NET 4.5.256 has not been registered on the Web server
  • 原文地址:https://www.cnblogs.com/ilvutm/p/7154220.html
Copyright © 2011-2022 走看看