关于mysql:mysql创建12个月的日期视图

46次阅读

共计 1754 个字符,预计需要花费 5 分钟才能阅读完成。

创立往年的 12 个月的视图:

CREATE 
    ALGORITHM = UNDEFINED 
    DEFINER = `root`@`%` 
    SQL SECURITY DEFINER
VIEW `year_month_view` AS
SELECT CONCAT(DATE_FORMAT((CURDATE()), '%Y'),'-01') AS date UNION
SELECT CONCAT(DATE_FORMAT((CURDATE()), '%Y'),'-02') AS date UNION
SELECT CONCAT(DATE_FORMAT((CURDATE()), '%Y'),'-03') AS date UNION
SELECT CONCAT(DATE_FORMAT((CURDATE()), '%Y'),'-04') AS date UNION
SELECT CONCAT(DATE_FORMAT((CURDATE()), '%Y'),'-05') AS date UNION
SELECT CONCAT(DATE_FORMAT((CURDATE()), '%Y'),'-06') AS date UNION
SELECT CONCAT(DATE_FORMAT((CURDATE()), '%Y'),'-07') AS date UNION
SELECT CONCAT(DATE_FORMAT((CURDATE()), '%Y'),'-08') AS date UNION
SELECT CONCAT(DATE_FORMAT((CURDATE()), '%Y'),'-09') AS date UNION
SELECT CONCAT(DATE_FORMAT((CURDATE()), '%Y'),'-10') AS date UNION
SELECT CONCAT(DATE_FORMAT((CURDATE()), '%Y'),'-11') AS date UNION
SELECT CONCAT(DATE_FORMAT((CURDATE()), '%Y'),'-12') AS date 

创立以后月前 12 个月的视图:

CREATE 
    ALGORITHM = UNDEFINED 
    DEFINER = `root`@`%` 
    SQL SECURITY DEFINER
VIEW `year_month_view` AS
    SELECT DATE_FORMAT(CURDATE(), '%Y-%m') AS `year_month` 
    UNION SELECT DATE_FORMAT((CURDATE() - INTERVAL 1 MONTH), '%Y-%m') AS `year_month` 
    UNION SELECT DATE_FORMAT((CURDATE() - INTERVAL 2 MONTH), '%Y-%m') AS `year_month` 
    UNION SELECT DATE_FORMAT((CURDATE() - INTERVAL 3 MONTH), '%Y-%m') AS `year_month` 
    UNION SELECT DATE_FORMAT((CURDATE() - INTERVAL 4 MONTH), '%Y-%m') AS `year_month` 
    UNION SELECT DATE_FORMAT((CURDATE() - INTERVAL 5 MONTH), '%Y-%m') AS `year_month` 
    UNION SELECT DATE_FORMAT((CURDATE() - INTERVAL 6 MONTH), '%Y-%m') AS `year_month` 
    UNION SELECT DATE_FORMAT((CURDATE() - INTERVAL 7 MONTH), '%Y-%m') AS `year_month` 
    UNION SELECT DATE_FORMAT((CURDATE() - INTERVAL 8 MONTH), '%Y-%m') AS `year_month` 
    UNION SELECT DATE_FORMAT((CURDATE() - INTERVAL 9 MONTH), '%Y-%m') AS `year_month` 
    UNION SELECT DATE_FORMAT((CURDATE() - INTERVAL 10 MONTH), '%Y-%m') AS `year_month` 
    UNION SELECT DATE_FORMAT((CURDATE() - INTERVAL 11 MONTH), '%Y-%m') AS `year_month`

正文完
 0