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

2023年 9月 23日 83.8k 0

如何借助 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 视图从日期范围生成天数?的详细内容,更多请关注每日运维网(www.mryunwei.com)其它相关文章!

相关文章

Oracle如何使用授予和撤销权限的语法和示例
Awesome Project: 探索 MatrixOrigin 云原生分布式数据库
下载丨66页PDF,云和恩墨技术通讯(2024年7月刊)
社区版oceanbase安装
Oracle 导出CSV工具-sqluldr2
ETL数据集成丨快速将MySQL数据迁移至Doris数据库

发布评论