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

时间:2012-07-11 16:18:32

标签: mysql calendar days

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

更新

我需要这样的清单:

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

3 个答案:

答案 0 :(得分: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-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>

试一试!!!

答案 1 :(得分: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

答案 2 :(得分: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');