登录
首页 >  数据库 >  MySQL

如何借助 MySQL 视图从日期范围生成天数?

来源:tutorialspoint

时间:2023-08-31 09:36:44 454浏览 收藏

哈喽!今天心血来潮给大家带来了《如何借助 MySQL 视图从日期范围生成天数?》,想必大家应该对数据库都不陌生吧,那么阅读本文就都不会很困难,以下内容主要涉及到,若是你正在学习数据库,千万别错过这篇文章~希望能帮助到你!

为了说明这一点,我们创建以下视图 -

mysql> CREATE VIEW digits AS
    -> SELECT 0 AS digit UNION ALL
    -> SELECT 1 UNION ALL
    -> SELECT 2 UNION ALL
    -> SELECT 3 UNION ALL
    -> SELECT 4 UNION ALL
    -> SELECT 5 UNION ALL
    -> SELECT 6 UNION ALL
    -> SELECT 7 UNION ALL
    -> SELECT 8 UNION ALL
    -> SELECT 9;
Query OK, 0 rows affected (0.08 sec)

mysql> CREATE VIEW numbers AS SELECT ones.digit + tens.digit * 10 + hundreds.digit * 100 + thousands.digit * 1000 AS number FROM digits as ones, digits as tens, digits as hundreds, digits as thousands;
Query OK, 0 rows affected (0.09 sec)

mysql> CREATE VIEW dates1 AS SELECT SUBDATE(CURRENT_DATE(), number) AS date FROM numbers UNION ALL SELECT ADDDATE(CURRENT_DATE(), number + 1) AS date FROM numbers;
Query OK, 0 rows affected (0.09 sec)

mysql> Select date from dates1 where date between '2017-11-15' and '2017-11-30'order by date;
+------------+
| date       |
+------------+
| 2017-11-15 |
| 2017-11-16 |
| 2017-11-17 |
| 2017-11-18 |
| 2017-11-19 |
| 2017-11-20 |
| 2017-11-21 |
| 2017-11-22 |
| 2017-11-23 |
| 2017-11-24 |
| 2017-11-25 |
| 2017-11-26 |
| 2017-11-27 |
| 2017-11-28 |
| 2017-11-29 |
| 2017-11-30 |
+------------+
16 rows in set (0.05 sec)

今天关于《如何借助 MySQL 视图从日期范围生成天数?》的内容就介绍到这里了,是不是学起来一目了然!想要了解更多关于的内容请关注golang学习网公众号!

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