1

是否有一个 MySQL 选择函数来列出任何给定月份的日期?

更新

我需要有这样的清单:

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

3 回答 3

1

这是一个纯 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 年 6 月 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>

试试看 !!!

于 2012-07-11T17:46:24.753 回答
0

尝试使用LAST_DAY('0000-00-00'), 并从您喜欢的语言 (PHP/ASP/WHATEVER) 循环从最后一天到第一天。

LAST_DAY()

获取日期或日期时间值并返回该月最后一天的相应值

来自:http ://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_last-day

于 2012-07-11T16:26:15.617 回答
0

我有一个名为 range 的表,其中包含从 0 到大约 100 的所有整数。它对这样的查询很有用:

SELECT '2012-07-01' + INTERVAL nr day AS day 
FROM test.range 
WHERE nr < 32 
HAVING MONTH(day) = MONTH('2012-07-01');
于 2012-07-11T17:02:57.820 回答