MySql教程

mysql从当前时间往前偏移24小时,和当天24小时sql

本文主要是介绍mysql从当前时间往前偏移24小时,和当天24小时sql,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

  当天24小时数据

SELECT  DATE_FORMAT( DATE_SUB( DATE_FORMAT( NOW(),'%Y-%m-%d'),INTERVAL ( -(@i:=@i+1) ) HOUR ) ,'%Y-%m-%d %H') AS 'time'
         FROM (
              SELECT a  FROM 
               (SELECT '1' AS a UNION SELECT '2' UNION SELECT '3' UNION SELECT '4'   ) AS a
                JOIN ( SELECT  '1' UNION SELECT '2' UNION SELECT '3' UNION SELECT '4' UNION SELECT '5' UNION SELECT '6' ) AS b
                 ON 1
               ) AS b  ,(SELECT @i:=-1)  AS i

  当前时间向前偏移24小时

SELECT  DATE_FORMAT( DATE_SUB( DATE_FORMAT( NOW(),'%Y-%m-%d %H'),INTERVAL ( (@i:=@i+1) ) HOUR ) ,'%H') AS 'time'
         FROM (
              SELECT a  FROM 
               (SELECT '1' AS a UNION SELECT '2' UNION SELECT '3' UNION SELECT '4'   ) AS a
                JOIN ( SELECT  '1' UNION SELECT '2' UNION SELECT '3' UNION SELECT '4' UNION SELECT '5' UNION SELECT '6' ) AS b
                 ON 1
               ) AS b  ,(SELECT @i:=-1)  AS i

  当前时间向前偏移7天

SELECT  DATE_FORMAT( DATE_SUB( DATE_FORMAT( NOW(),'%Y-%m-%d'),INTERVAL ( (@i:=@i+1) ) DAY ) ,'%d') AS 'time'
         FROM (
              SELECT a  FROM 
               (SELECT '1' AS a UNION SELECT '2' UNION SELECT '3' UNION SELECT '4' UNION SELECT '5' UNION SELECT '6' UNION SELECT '7' ) AS a
               ) AS b  ,(SELECT @i:=-1)  AS i

  

这篇关于mysql从当前时间往前偏移24小时,和当天24小时sql的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!