• MySql查询当天、本周、本月、本季度、本年的数据


    1.今天

    SELECT * FROM 表名 WHERE TO_DAYS(时间字段名) = TO_DAYS(NOW());

    2.昨天

    SELECT * FROM 表名 WHERE TO_DAYS(NOW()) - TO_DAYS(时间字段名) <= 1;

    3.本周

    SELECT * FROM 表名 WHERE YEARWEEK(DATE_FORMAT(时间字段名,'%Y-%m-%d')) = YEARWEEK(NOW());

    4.上周

    SELECT * FROM 表名 WHERE YEARWEEK(DATE_FORMAT(时间字段名,'%Y-%m-%d')) = YEARWEEK(NOW())-1;

    5.近7天

    SELECT * FROM 表名 WHERE DATE_SUB(CURDATE(), INTERVAL 7 DAY) <= DATE(时间字段名);

    6.近30天

    SELECT * FROM 表名 WHERE DATE_SUB(CURDATE(), INTERVAL 30 DAY) <= DATE(时间字段名);

    7.本月

    SELECT * FROM 表名 WHERE DATE_FORMAT(时间字段名,'%Y%m') = DATE_FORMAT(CURDATE(),'%Y%m');

    8.上月

    SELECT * FROM 表名 WHERE PERIOD_DIFF(DATE_FORMAT(NOW(),'%Y%m'),DATE_FORMAT(时间字段名,'%Y%m')) = 1;
    
    SELECT * FROM 表名 WHERE DATE_FORMAT(时间字段名,'%Y%m') = DATE_FORMAT(CURDATE(),'%Y%m') ; 
    
    SELECT * FROM 表名 WHERE WEEKOFYEAR(FROM_UNIXTIME(时间字段名,'%y-%m-%d')) = WEEKOFYEAR(NOW()); 
    
    SELECT * FROM 表名 WHERE MONTH(FROM_UNIXTIME(时间字段名,'%y-%m-%d')) = MONTH(NOW()); 
    
    SELECT * FROM 表名 WHERE YEAR(FROM_UNIXTIME(时间字段名,'%y-%m-%d')) = YEAR(NOW()) AND MONTH(FROM_UNIXTIME(时间字段名,'%y-%m-%d')) = MONTH(NOW());

    9.近6个月

    SELECT * FROM 表名 WHERE 时间字段名 BETWEEN DATE_SUB(NOW(),INTERVAL 6 MONTH) AND NOW();

    10.本季度

    SELECT * FROM 表名 WHERE QUARTER(时间字段名) = QUARTER(NOW());

    11.上季度

    SELECT * FROM 表名 WHERE QUARTER(时间字段名) = QUARTER(DATE_SUB(NOW(),INTERVAL 1 QUARTER));

    12.本年

    SELECT * FROM 表名 WHERE YEAR(时间字段名)=YEAR(NOW());

    13.去年

    SELECT * FROM 表名 WHERE YEAR(时间字段名) = YEAR(DATE_SUB(NOW(),INTERVAL 1 YEAR));
  • 相关阅读:
    ad_imh
    pc send instructor pc ad
    数据、模型、IT系统认知
    量化投资认知
    LinAlgError: Last 2 dimensions of the array must be square
    转:Hadoop大数据开发基础系列:七、Hive基础
    Run-Time Check Failure #2
    0x00007FFC8C5325E7 (ucrtbased.dll)处(位于 DataStructure.exe 中)引发的异常: 0xC0000005: 读取位置 0xFFFFFFFFFFFFFFFF 时发生访问冲突。
    栈与后缀表达式C实现
    Jupyter使用
  • 原文地址:https://www.cnblogs.com/cui0614/p/12091431.html
Copyright © 2020-2023  润新知