2020-10-20 痛定思痛。 2023-10-05 12:06 15阅读 0赞 [\[MySQL获取某个时间范围内的数据 TO\_DAYS(date)函数\]][MySQL_ TO_DAYS_date] 单纯个人笔记 ! 1、利用to\_days函数查询今天的数据: select \* from 表名 where to\_days(时间字段名) = to\_days(now()); to\_days函数:返回从0000年(公元1年)至当前日期的总天数。 2、昨天 SELECT \* FROM 表名 WHERE TO\_DAYS( NOW( ) ) – TO\_DAYS( 时间字段名) <= 1 3.7天 SELECT \* FROM 表名 where DATE\_SUB(CURDATE(), INTERVAL 7 DAY) <= date(时间字段名) 4.近30天 SELECT \* FROM 表名 where DATE\_SUB(CURDATE(), INTERVAL 30 DAY) <= date(时间字段名) 5.本月 SELECT \* FROM 表名 WHERE DATE\_FORMAT( 时间字段名, ‘%Y%m’ ) = DATE\_FORMAT( CURDATE( ) , ‘%Y%m’ ) 6.上一月 SELECT \* FROM 表名 WHERE PERIOD\_DIFF( date\_format( now( ) , ‘%Y%m’ ) , date\_format( 时间字段名, ‘%Y%m’ ) ) =1 \#查询本季度数据 select \* from `ht_invoice_information` where QUARTER(create\_date)=QUARTER(now()); \#查询上季度数据 select \* from `ht_invoice_information` where QUARTER(create\_date)=QUARTER(DATE\_SUB(now(),interval 1 QUARTER)); \#查询本年数据 select \* from `ht_invoice_information` where YEAR(create\_date)=YEAR(NOW()); \#查询上年数据 select \* from `ht_invoice_information` where year(create\_date)=year(date\_sub(now(),interval 1 year)); 查询当前这周的数据 SELECT name,submittime FROM enterprise WHERE YEARWEEK(date\_format(submittime,’%Y-%m-%d’)) = YEARWEEK(now()); 查询上周的数据 SELECT name,submittime FROM enterprise WHERE YEARWEEK(date\_format(submittime,’%Y-%m-%d’)) = YEARWEEK(now())-1; 查询当前月份的数据 select name,submittime from enterprise where date\_format(submittime,’%Y-%m’)=date\_format(now(),’%Y-%m’) 查询距离当前现在6个月的数据 select name,submittime from enterprise where submittime between date\_sub(now(),interval 6 month) and now(); 查询上个月的数据 select name,submittime from enterprise where date\_format(submittime,’%Y-%m’)=date\_format(DATE\_SUB(curdate(), INTERVAL 1 MONTH),’%Y-%m’) select \* from `user` where DATE\_FORMAT(pudate, ‘ %Y%m ‘ ) = DATE\_FORMAT(CURDATE(), ‘ %Y%m ‘ ) ; select \* from user where WEEKOFYEAR(FROM\_UNIXTIME(pudate,’%y-%m-%d’)) = WEEKOFYEAR(now()) select \* from user where MONTH (FROM\_UNIXTIME(pudate, ‘ %y-%m-%d ‘ )) = MONTH (now()) select \* from \[ user \] where YEAR (FROM\_UNIXTIME(pudate, ‘ %y-%m-%d ‘ )) = YEAR (now()) and MONTH (FROM\_UNIXTIME(pudate, ‘ %y-%m-%d ‘ )) = MONTH (now()) select \* from \[ user \] where pudate between 上月最后一天 and 下月第一天 where date(regdate) = curdate(); select \* from test where year(regdate)=year(now()) and month(regdate)=month(now()) and day(regdate)=day(now()) SELECT date( c\_instime ) ,curdate( ) FROM `t_score` WHERE 1 LIMIT 0 , 30 [MySQL_ TO_DAYS_date]: https://www.cnblogs.com/qcxdoit/p/7085799.html
相关 Oracle 11.2.0 PSU补丁(20201020) 准备补丁包 mkdir /tmp/psu 上传补丁包 备份原有目录 mv /opt/grid/products/11.2.0/OPatch 妖狐艹你老母/ 2022年08月28日 13:55/ 0 赞/ 132 阅读
还没有评论,来说两句吧...