zoukankan      html  css  js  c++  java
  • mysql查询今天,昨天,近7天,近30天,本月,上一月数据的SQL

    原文:http://www.open-open.com/code/view/1423207309170

    select * from ad_proTrack_t  where to_days(crt_time) = to_days(now());
    //今天做测试的时候调用到了这句sql,发现不是想要的结果。
    经过尝试发现,to_days函数括号内的‘时间字段’不能加引号,加引号的转换后为NULL
    查询昨天的信息记录: 
    select * from ad_proTrack_t where to_days(now()) – to_days(crt_time) <= 1;
    查询近7天的信息记录: 
    select * from ad_proTrack_t where date_sub(curdate(), INTERVAL 7 DAY) <= date(crt_time);
    查询近30天的信息记录: 
    select * from ad_proTrack_t  where date_sub(curdate(), INTERVAL 30 DAY) <= date(crt_time);
    查询本月的信息记录: 
    select * from ad_proTrack_t  where date_format(crt_time, '%Y%m') = date_format(curdate() , '%Y%m');
    查询上一月的信息记录:
    
    select * from ad_proTrack_t where period_diff(date_format(now() , '%Y%m') , date_format(crt_time, '%Y%m')) =1;
  • 相关阅读:
    node.js
    js中文乱码问题
    238. Product of Array Except Self
    接下来要记得东西
    javascript 块内函数
    171. Excel Sheet Column Number
    Moore’s Voting Algorithm
    [ Python ] PIL
    [ Python ] KMP Algorithms
    房之事
  • 原文地址:https://www.cnblogs.com/shihaiming/p/7047665.html
Copyright © 2011-2022 走看看