0

查询生成 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)
4

5 回答 5

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

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

于 2013-08-20T11:56:58.527 回答
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())

这给出了输出

于 2013-08-20T11:46:41.923 回答
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
于 2013-08-20T11:55:28.577 回答
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 中这样做)

于 2013-08-20T12:11:10.863 回答
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
于 2013-08-20T12:06:15.107 回答