gpt4 book ai didi

MySQL - 如何确定任何给定月份的天数列表

转载 作者:行者123 更新时间:2023-11-29 00:43:02 24 4
gpt4 key购买 nike

是否有 MySQL 选择函数来列出任何给定月份的天数?

更新

我需要这样的列表:

|day|
|1 |
|2 |
|3 |
|. |
|. |
|30 |
|31 |

最佳答案

这是一个纯 MySQL 解决方案,用于生成查询字符串以生成日期列表

SET @GivenDate = '2012-02-26';
SELECT DAY(CONCAT(DATE_FORMAT(dt28 + INTERVAL 4 DAY,
'%Y-%m'),'-01') - INTERVAL 1 DAY) - 27 INTO @DaysAfter27
FROM (SELECT CONCAT(DATE_FORMAT(dt,'%Y-%m'),'-28') dt28
FROM (SELECT @GivenDate dt) AA) A;
SELECT ELT(@DaysAfter27,' UNION (SELECT 28)',
' UNION (SELECT 28) UNION (SELECT 29)',
' UNION (SELECT 28) UNION (SELECT 29) UNION (SELECT 30)',
' UNION (SELECT 28) UNION (SELECT 29) UNION (SELECT 30) UNION (SELECT 31)')
INTO @DaysEndOfList;
SELECT CONCAT('SELECT * FROM',
' ((SELECT 1 Days) UNION (SELECT 2) UNION (SELECT 3)'
' UNION (SELECT 4) UNION (SELECT 5) UNION (SELECT 6)',
' UNION (SELECT 7) UNION (SELECT 8) UNION (SELECT 9)',
' UNION (SELECT 10) UNION (SELECT 11) UNION (SELECT 12)',
' UNION (SELECT 13) UNION (SELECT 14) UNION (SELECT 15)',
' UNION (SELECT 16) UNION (SELECT 17) UNION (SELECT 18)',
' UNION (SELECT 19) UNION (SELECT 20) UNION (SELECT 21)',
' UNION (SELECT 22) UNION (SELECT 23) UNION (SELECT 24)',
' UNION (SELECT 25) UNION (SELECT 26) UNION (SELECT 27)',
@DaysEndOfList,') A') INTO @DaysQuery;
PREPARE s1 FROM @DaysQuery;
EXECUTE s1;
DEALLOCATE PREPARE s1;

只需将@GivenDate 设置为您喜欢的任何日期

这是我设置@GivenDate 并执行它的四个示例

示例 1:2012-02-26

mysql> SET @GivenDate = '2012-02-26';
Query OK, 0 rows affected (0.00 sec)

mysql> SELECT DAY(CONCAT(DATE_FORMAT(dt28 + INTERVAL 4 DAY,
-> '%Y-%m'),'-01') - INTERVAL 1 DAY) - 27 INTO @DaysAfter27
-> FROM (SELECT CONCAT(DATE_FORMAT(dt,'%Y-%m'),'-28') dt28
-> FROM (SELECT @GivenDate dt) AA) A;
Query OK, 1 row affected (0.00 sec)

mysql> SELECT ELT(@DaysAfter27,' UNION (SELECT 28)',
-> ' UNION (SELECT 28) UNION (SELECT 29)',
-> ' UNION (SELECT 28) UNION (SELECT 29) UNION (SELECT 30)',
-> ' UNION (SELECT 28) UNION (SELECT 29) UNION (SELECT 30) UNION (SELECT 31)')
-> INTO @DaysEndOfList;
Query OK, 1 row affected (0.00 sec)

mysql> SELECT CONCAT('SELECT * FROM',
-> ' ((SELECT 1 Days) UNION (SELECT 2) UNION (SELECT 3)'
-> ' UNION (SELECT 4) UNION (SELECT 5) UNION (SELECT 6)',
-> ' UNION (SELECT 7) UNION (SELECT 8) UNION (SELECT 9)',
-> ' UNION (SELECT 10) UNION (SELECT 11) UNION (SELECT 12)',
-> ' UNION (SELECT 13) UNION (SELECT 14) UNION (SELECT 15)',
-> ' UNION (SELECT 16) UNION (SELECT 17) UNION (SELECT 18)',
-> ' UNION (SELECT 19) UNION (SELECT 20) UNION (SELECT 21)',
-> ' UNION (SELECT 22) UNION (SELECT 23) UNION (SELECT 24)',
-> ' UNION (SELECT 25) UNION (SELECT 26) UNION (SELECT 27)',
-> @DaysEndOfList,') A') INTO @DaysQuery;
Query OK, 1 row affected (0.00 sec)

mysql> PREPARE s1 FROM @DaysQuery;
Query OK, 0 rows affected (0.00 sec)
Statement prepared

mysql> EXECUTE s1;
+------+
| Days |
+------+
| 1 |
| 2 |
| 3 |
| 4 |
| 5 |
| 6 |
| 7 |
| 8 |
| 9 |
| 10 |
| 11 |
| 12 |
| 13 |
| 14 |
| 15 |
| 16 |
| 17 |
| 18 |
| 19 |
| 20 |
| 21 |
| 22 |
| 23 |
| 24 |
| 25 |
| 26 |
| 27 |
| 28 |
| 29 |
+------+
29 rows in set (0.00 sec)

mysql> DEALLOCATE PREPARE s1;
Query OK, 0 rows affected (0.00 sec)

mysql>

示例 2:2011-02-26

mysql> SET @GivenDate = '2011-02-26';
Query OK, 0 rows affected (0.00 sec)

mysql> SELECT DAY(CONCAT(DATE_FORMAT(dt28 + INTERVAL 4 DAY,
-> '%Y-%m'),'-01') - INTERVAL 1 DAY) - 27 INTO @DaysAfter27
-> FROM (SELECT CONCAT(DATE_FORMAT(dt,'%Y-%m'),'-28') dt28
-> FROM (SELECT @GivenDate dt) AA) A;
Query OK, 1 row affected (0.00 sec)

mysql> SELECT ELT(@DaysAfter27,' UNION (SELECT 28)',
-> ' UNION (SELECT 28) UNION (SELECT 29)',
-> ' UNION (SELECT 28) UNION (SELECT 29) UNION (SELECT 30)',
-> ' UNION (SELECT 28) UNION (SELECT 29) UNION (SELECT 30) UNION (SELECT 31)')
-> INTO @DaysEndOfList;
Query OK, 1 row affected (0.00 sec)

mysql> SELECT CONCAT('SELECT * FROM',
-> ' ((SELECT 1 Days) UNION (SELECT 2) UNION (SELECT 3)'
-> ' UNION (SELECT 4) UNION (SELECT 5) UNION (SELECT 6)',
-> ' UNION (SELECT 7) UNION (SELECT 8) UNION (SELECT 9)',
-> ' UNION (SELECT 10) UNION (SELECT 11) UNION (SELECT 12)',
-> ' UNION (SELECT 13) UNION (SELECT 14) UNION (SELECT 15)',
-> ' UNION (SELECT 16) UNION (SELECT 17) UNION (SELECT 18)',
-> ' UNION (SELECT 19) UNION (SELECT 20) UNION (SELECT 21)',
-> ' UNION (SELECT 22) UNION (SELECT 23) UNION (SELECT 24)',
-> ' UNION (SELECT 25) UNION (SELECT 26) UNION (SELECT 27)',
-> @DaysEndOfList,') A') INTO @DaysQuery;
Query OK, 1 row affected (0.00 sec)

mysql> PREPARE s1 FROM @DaysQuery;
Query OK, 0 rows affected (0.00 sec)
Statement prepared

mysql> EXECUTE s1;
+------+
| Days |
+------+
| 1 |
| 2 |
| 3 |
| 4 |
| 5 |
| 6 |
| 7 |
| 8 |
| 9 |
| 10 |
| 11 |
| 12 |
| 13 |
| 14 |
| 15 |
| 16 |
| 17 |
| 18 |
| 19 |
| 20 |
| 21 |
| 22 |
| 23 |
| 24 |
| 25 |
| 26 |
| 27 |
| 28 |
+------+
28 rows in set (0.00 sec)

mysql> DEALLOCATE PREPARE s1;
Query OK, 0 rows affected (0.01 sec)

mysql>

示例 3:今天 (2012-07-11)

mysql> SET @GivenDate = DATE(NOW());
Query OK, 0 rows affected (0.00 sec)

mysql> SELECT DAY(CONCAT(DATE_FORMAT(dt28 + INTERVAL 4 DAY,
-> '%Y-%m'),'-01') - INTERVAL 1 DAY) - 27 INTO @DaysAfter27
-> FROM (SELECT CONCAT(DATE_FORMAT(dt,'%Y-%m'),'-28') dt28
-> FROM (SELECT @GivenDate dt) AA) A;
Query OK, 1 row affected (0.00 sec)

mysql> SELECT ELT(@DaysAfter27,' UNION (SELECT 28)',
-> ' UNION (SELECT 28) UNION (SELECT 29)',
-> ' UNION (SELECT 28) UNION (SELECT 29) UNION (SELECT 30)',
-> ' UNION (SELECT 28) UNION (SELECT 29) UNION (SELECT 30) UNION (SELECT 31)')
-> INTO @DaysEndOfList;
Query OK, 1 row affected (0.00 sec)

mysql> SELECT CONCAT('SELECT * FROM',
-> ' ((SELECT 1 Days) UNION (SELECT 2) UNION (SELECT 3)'
-> ' UNION (SELECT 4) UNION (SELECT 5) UNION (SELECT 6)',
-> ' UNION (SELECT 7) UNION (SELECT 8) UNION (SELECT 9)',
-> ' UNION (SELECT 10) UNION (SELECT 11) UNION (SELECT 12)',
-> ' UNION (SELECT 13) UNION (SELECT 14) UNION (SELECT 15)',
-> ' UNION (SELECT 16) UNION (SELECT 17) UNION (SELECT 18)',
-> ' UNION (SELECT 19) UNION (SELECT 20) UNION (SELECT 21)',
-> ' UNION (SELECT 22) UNION (SELECT 23) UNION (SELECT 24)',
-> ' UNION (SELECT 25) UNION (SELECT 26) UNION (SELECT 27)',
-> @DaysEndOfList,') A') INTO @DaysQuery;
Query OK, 1 row affected (0.00 sec)

mysql> PREPARE s1 FROM @DaysQuery;
Query OK, 0 rows affected (0.00 sec)
Statement prepared

mysql> EXECUTE s1;
+------+
| Days |
+------+
| 1 |
| 2 |
| 3 |
| 4 |
| 5 |
| 6 |
| 7 |
| 8 |
| 9 |
| 10 |
| 11 |
| 12 |
| 13 |
| 14 |
| 15 |
| 16 |
| 17 |
| 18 |
| 19 |
| 20 |
| 21 |
| 22 |
| 23 |
| 24 |
| 25 |
| 26 |
| 27 |
| 28 |
| 29 |
| 30 |
| 31 |
+------+
31 rows in set (0.00 sec)

mysql> DEALLOCATE PREPARE s1;
Query OK, 0 rows affected (0.00 sec)

mysql>

示例 4:2012-06-26:

mysql> SET @GivenDate = '2012-06-26';
Query OK, 0 rows affected (0.00 sec)

mysql> SELECT DAY(CONCAT(DATE_FORMAT(dt28 + INTERVAL 4 DAY,
-> '%Y-%m'),'-01') - INTERVAL 1 DAY) - 27 INTO @DaysAfter27
-> FROM (SELECT CONCAT(DATE_FORMAT(dt,'%Y-%m'),'-28') dt28
-> FROM (SELECT @GivenDate dt) AA) A;
Query OK, 1 row affected (0.00 sec)

mysql> SELECT ELT(@DaysAfter27,' UNION (SELECT 28)',
-> ' UNION (SELECT 28) UNION (SELECT 29)',
-> ' UNION (SELECT 28) UNION (SELECT 29) UNION (SELECT 30)',
-> ' UNION (SELECT 28) UNION (SELECT 29) UNION (SELECT 30) UNION (SELECT 31)')
-> INTO @DaysEndOfList;
Query OK, 1 row affected (0.00 sec)

mysql> SELECT CONCAT('SELECT * FROM',
-> ' ((SELECT 1 Days) UNION (SELECT 2) UNION (SELECT 3)'
-> ' UNION (SELECT 4) UNION (SELECT 5) UNION (SELECT 6)',
-> ' UNION (SELECT 7) UNION (SELECT 8) UNION (SELECT 9)',
-> ' UNION (SELECT 10) UNION (SELECT 11) UNION (SELECT 12)',
-> ' UNION (SELECT 13) UNION (SELECT 14) UNION (SELECT 15)',
-> ' UNION (SELECT 16) UNION (SELECT 17) UNION (SELECT 18)',
-> ' UNION (SELECT 19) UNION (SELECT 20) UNION (SELECT 21)',
-> ' UNION (SELECT 22) UNION (SELECT 23) UNION (SELECT 24)',
-> ' UNION (SELECT 25) UNION (SELECT 26) UNION (SELECT 27)',
-> @DaysEndOfList,') A') INTO @DaysQuery;
Query OK, 1 row affected (0.00 sec)

mysql> PREPARE s1 FROM @DaysQuery;
Query OK, 0 rows affected (0.00 sec)
Statement prepared

mysql> EXECUTE s1;
+------+
| Days |
+------+
| 1 |
| 2 |
| 3 |
| 4 |
| 5 |
| 6 |
| 7 |
| 8 |
| 9 |
| 10 |
| 11 |
| 12 |
| 13 |
| 14 |
| 15 |
| 16 |
| 17 |
| 18 |
| 19 |
| 20 |
| 21 |
| 22 |
| 23 |
| 24 |
| 25 |
| 26 |
| 27 |
| 28 |
| 29 |
| 30 |
+------+
30 rows in set (0.00 sec)

mysql> DEALLOCATE PREPARE s1;
Query OK, 0 rows affected (0.00 sec)

mysql>

试一试!!!

关于MySQL - 如何确定任何给定月份的天数列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11437003/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com