安信科技官網(wǎng)-觸屏版

      當(dāng)前位置:首頁(yè) > 安信自媒體 > 建站經(jīng)驗(yàn)

      MySql 時(shí)間操作詳解

      時(shí)間:2021年10月24日 瀏覽:
      MySql 時(shí)間操作(今天,昨天,7天,30天,本月,上月)
      1 、 查看當(dāng)天日期

      selec t current_date();

       

      2、 查看當(dāng)天時(shí)間

      selec t current_time();

       

      3、查看當(dāng)天時(shí)間日期

      selec t current_timestamp();

       

      4、查詢當(dāng)天記錄

      selec t * from 表名 where to_days(時(shí)間字段名) = to_days(now());

       

      5、查詢昨天記錄

      selec t * FROM 表名 WHERE TO_DAYS( NOW( ) ) – TO_DAYS( 時(shí)間字段名) <= 1

       

      6、查詢7天的記錄

      selec t * FROM 表名 where DATE_SUB(CURDATE(), INTERVAL 7 DAY) <= date(時(shí)間字段名) 

       

      7、查詢近30天的記錄

      selec t * FROM 表名 where DATE_SUB(CURDATE(), INTERVAL 30 DAY) <= date(時(shí)間字段名)

       

      8、查詢本月的記錄

      selec t * FROM 表名 WHERE DATE_FORMAT( 時(shí)間字段名, ‘%Y%m’ ) = DATE_FORMAT( CURDATE( ) , ‘%Y%m’ )

       

      9、查詢上一月的記錄

      selec t * FROM 表名 WHERE PERIOD_DIFF( date_format( now( ) , ‘%Y%m’ ) , date_format( 時(shí)間字段名, ‘%Y%m’ ) ) =1

       

      10、查詢本季度數(shù)據(jù)

      selec t * from 表名 where QUARTER(create_date)=QUARTER(now());


      11、查詢上季度數(shù)據(jù)
      selec t * from 表名 where QUARTER(create_date)=QUARTER(DATE_SUB(now(),interval 1 QUARTER));

       

      12、查詢本年數(shù)據(jù)
      selec t * from 表名  where YEAR(create_date)=YEAR(NOW());

       

      13、查詢上年數(shù)據(jù)
      selec t * from 表名 where year(create_date)=year(date_sub(now(),interval 1 year));
       
      14、查詢當(dāng)前這周的數(shù)據(jù) 
      selec t * FROM 表名 WHERE YEARWEEK(date_format(submittime,'%Y-%m-%d')) = YEARWEEK(now());

       

      15、查詢上周的數(shù)據(jù)
      selec t * FROM 表名 WHERE YEARWEEK(date_format(submittime,'%Y-%m-%d')) = YEARWEEK(now())-1;

       

      16、查詢當(dāng)前月份的數(shù)據(jù)
      selec t * from 表名   where date_format(submittime,'%Y-%m')=date_format(now(),'%Y-%m')

       

      17、查詢距離當(dāng)前現(xiàn)在6個(gè)月的數(shù)據(jù)
      selec t name,submittime from enterprise where submittime between date_sub(now(),interval 6 month) and now();

      當(dāng)前頁(yè)面電腦端訪問網(wǎng)址:


      發(fā)送給朋友 分享到朋友圈

      手機(jī)版電腦版