选择日期直到当前日期 - mysql

时间:2013-08-20 11:25:20

标签: mysql

今天是2013年8月20日。 我希望使用查询生成20行,其中包含该月份的第1到第20个日期(无论当前日期是什么)。 计数应始终从月的第一个日期开始,直到当前日期...输出将是,只有一列和多行,直到当前日期如下所示..

    Current month
    8/1/13 12:00 AM
    8/2/13 12:00 AM
    8/3/13 12:00 AM
    8/4/13 12:00 AM
    8/5/13 12:00 AM
    8/6/13 12:00 AM
    8/7/13 12:00 AM
    8/8/13 12:00 AM
    8/9/13 12:00 AM
    8/10/13 12:00 AM
    8/11/13 12:00 AM
    8/12/13 12:00 AM
    8/13/13 12:00 AM
    8/14/13 12:00 AM
    8/15/13 12:00 AM
    8/16/13 12:00 AM
    8/17/13 12:00 AM
    8/18/13 12:00 AM
    8/19/13 12:00 AM
    8/20/13 12:00 AM

我尝试了以下查询,但没有用。你能帮忙找一些其他的解决方法吗?

DECLARE @startDate DATETIME=CAST(MONTH(GETDATE()) AS VARCHAR) + '/' + '01/' +  + CAST(YEAR(GETDATE()) AS VARCHAR) -- mm/dd/yyyy
DECLARE @endDate DATETIME= GETDATE() -- mm/dd/yyyy

;WITH Calender AS 
(
    SELECT @startDate AS CalanderDate
    UNION ALL
    SELECT CalanderDate + 1 FROM Calender
    WHERE CalanderDate + 1 <= @endDate
)
SELECT [Date] = CONVERT(VARCHAR(10),CalanderDate,25) 
FROM Calender
OPTION (MAXRECURSION 0)

5 个答案:

答案 0 :(得分:2)

这也可以通过动态构建所有日期的结果集来实现,但是可以对任何至少有31天(任何给定月份的最大值)的现有表格起作用。

select
      @curDay := date_add( @curDay, interval 1 day ) as CalendarDay
   from
      ( select @curDay := date_add( DATE_FORMAT(NOW(),
         '%Y-%m-01'), interval -1 day) ) sqlvars,
      AnyTableInYourDatabaseWithAtLeast31Records
   where
      @curDay <= now()
   limit
      31

选择@curDay的第一部分构建当天的任何内容,到达本月的第一天,然后从中减去1天,为您提供上个月的最后一天。然后,外部选择@curDay:=通过添加1天作为CalendarDay结果列来不断更新自身。由于它连接到数据库中的“任何表”,它将继续获取31条记录的最大值,但只返回日期小于或当前的记录。

答案 1 :(得分:0)

小提琴

http://www.sqlfiddle.com/#!2/28466/1

CREATE TABLE CALENDAR(DATE1 DATETIME);

INSERT INTO CALENDAR VALUES ('2013/8/1 12:00:00');
INSERT INTO CALENDAR VALUES ('2013/8/2 12:00:00');
INSERT INTO CALENDAR VALUES ('2013/8/3 12:00:00');
INSERT INTO CALENDAR VALUES ('2013/8/4 12:00:00');....

SELECT DISTINCT DATE1 FROM Calender where MONTH(DATE1)=MONTH(NOW()) and DAYOFMONTH(DATE1) <=DAYOFMONTH(NOW())

这给出了输出

答案 2 :(得分:0)

我喜欢使用计数表来解决这些问题,它们往往非常快:

DECLARE @startDate DATETIME= CONVERT(VARCHAR(25),DATEADD(dd,-(DAY(GETDATE())-1),GETDATE()),101);
WITH 
 N0 as (SELECT 1 as n UNION ALL SELECT 1)
,N1 as (SELECT 1 as n FROM N0 AS t1 CROSS JOIN N0 AS t2)
,N2 as (SELECT 1 as n FROM N1 AS t1 CROSS JOIN N1 AS t2)
,N3 as (SELECT 1 as n FROM N2 AS t1 CROSS JOIN N2 AS t2)
,N4 as (SELECT 1 as n FROM N3 AS t1 CROSS JOIN N3 AS t2)
,N5 as (SELECT 1 as n FROM N4 AS t1 CROSS JOIN N4 AS t2)
,N6 as (SELECT 1 as n FROM N5 AS t1 CROSS JOIN N5 AS t2)
,nums as (SELECT ROW_NUMBER() OVER (ORDER BY (SELECT 1)) as num FROM N6)
SELECT DATEADD(day,num-1,@startDate) as theDate
FROM nums
WHERE num <= DATEDIFF(day,@startDate,GETDATE()) + 1

答案 3 :(得分:0)

您可以尝试调用此存储过程;

    DELIMITER $$

    CREATE PROCEDURE `test`.`GenerateDates` ()

BEGIN
    DECLARE Days INTEGER;
    DECLARE Count INTEGER;  

    SET Days = DATEDIFF(NOW(),CONCAT(YEAR(NOW()),'-',MONTH(NOW()),'-01'));
    SET Count = 0;

    DROP TEMPORARY TABLE IF EXISTS tempDates;
    CREATE TEMPORARY TABLE tempDates
        (
            YourDate Date,
            PRIMARY KEY(YourDate)
        );


    WHILE (Count <= Days) DO
       INSERT INTO tempDates (YourDate) VALUES
       (DATE_FORMAT(DATE_ADD(CONCAT(YEAR(NOW()),'-',MONTH(NOW()),'-01'), INTERVAL Count DAY),'%Y-%m-%d'));
       SET Count = Count + 1;
    END WHILE;

    SELECT * FROM tempDates;
END

答案 4 :(得分:0)

:) ...或者如果一个包含31个整数的表似乎是一个延伸,那么10个表的表格怎么样......

SELECT * FROM ints;
+---+
| i |
+---+
| 0 |
| 1 |
| 2 |
| 3 |
| 4 |
| 5 |
| 6 |
| 7 |
| 8 |
| 9 |
+---+

SELECT DATE_FORMAT(CURDATE(),'%Y-%m-01')+INTERVAL i2.i*10+i1.i DAY x FROM ints i1, ints i2 HAVING x <= NOW();
+------------+
| x          |
+------------+
| 2013-08-01 |
| 2013-08-02 |
| 2013-08-03 |
| 2013-08-04 |
| 2013-08-05 |
| 2013-08-06 |
| 2013-08-07 |
| 2013-08-08 |
| 2013-08-09 |
| 2013-08-10 |
| 2013-08-11 |
| 2013-08-12 |
| 2013-08-13 |
| 2013-08-14 |
| 2013-08-15 |
| 2013-08-16 |
| 2013-08-17 |
| 2013-08-18 |
| 2013-08-19 |
| 2013-08-20 |
+------------+

(仍然不确定为什么你会在MySQL中这样做)