我想在mysql的日期范围内生成一个日期。
我无法解释我真正需要做的事情,所以我这样说。
SELECT CAST(BETWEEN STR_TO_DATE('05/06/2015','%m/%d/%Y') AND STR_TO_DATE('05/20/2015','%m/%d/%Y') AS DATE)
输出应为:
答案 0 :(得分:1)
假设您有权创建和执行存储过程,您可以这样做:
awoolford$ mysql -u root -p
Enter password:
mysql> use test;
mysql> delimiter //
mysql> CREATE PROCEDURE createDateRange (IN startDate DATE, IN endDate DATE)
-> BEGIN
-> DECLARE currentDate DATE;
-> SET currentDate := startDate;
-> DROP TABLE IF EXISTS dateRange;
-> CREATE TABLE dateRange(`date` date);
-> WHILE currentDate <= endDate DO
-> INSERT INTO dateRange VALUES (currentDate);
-> SET currentDate := DATE_ADD(currentDate, INTERVAL 1 DAY);
-> END WHILE;
-> END//
mysql> delimiter ;
mysql> CALL createDateRange('2015-04-24', '2015-05-01');
Query OK, 1 row affected (0.01 sec)
mysql> select * from dateRange;
+------------+
| date |
+------------+
| 2015-04-24 |
| 2015-04-25 |
| 2015-04-26 |
| 2015-04-27 |
| 2015-04-28 |
| 2015-04-29 |
| 2015-04-30 |
| 2015-05-01 |
+------------+
8 rows in set (0.00 sec)