登录
首页 >  数据库 >  MySQL

mysql创建12个月的日期视图

来源:SegmentFault

时间:2023-02-16 15:47:36 436浏览 收藏

你在学习数据库相关的知识吗?本文《mysql创建12个月的日期视图》,主要介绍的内容就涉及到MySQL、视图,如果你想提升自己的开发能力,就不要错过这篇文章,大家要知道编程理论基础和实战操作都是不可或缺的哦!

创建今年的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`

文中关于mysql的知识介绍,希望对你的学习有所帮助!若是受益匪浅,那就动动鼠标收藏这篇《mysql创建12个月的日期视图》文章吧,也可关注golang学习网公众号了解相关技术文章。

声明:本文转载于:SegmentFault 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>
评论列表