1

我的表中有以下数据。顺便说一句...这是 DD/MM/YYYY 格式:

Date
18/09/2012
17/09/2012
13/09/2012
11/09/2012
10/09/2012
09/09/2012
25/08/2012
24/08/2012

我想要的结果是:

Date
18/09/2012
13/09/2012
11/09/2012
09/09/2012
25/08/2012

规则:它从最晚的日期(18/09/2012)开始,并检查下一个日期(17/09/2012)。如果有日期,则将其从列表中删除,因为它需要相隔 1 天。然后转到 2012 年 9 月 13 日,然后检查 2012 年 9 月 12 日,没有找到,然后转到下一个,依此类推。基本上你不能让日期彼此接近(至少相隔 1 天)。

现在,如果它在 TSQL 上,我可以在光标上执行此操作,但是由于我正在使用 MySQL,MySQL 中是否有这样的事情?或者也许任何可以解决此查询的子查询方法?

我很感激你的反馈。

4

3 回答 3

1
SELECT
    "MyTable1"."Date"
FROM
    "MyTable" AS "MyTable1"
    LEFT JOIN "MyTable" AS "MyTable2" ON
        ADDDATE("MyTable1"."Date", INTERVAL 1 DAY) = "MyTable2"."Date"
WHERE
    "MyTable2"."Date" IS NULL
ORDER BY
    "MyTable1"."Date" DESC
于 2012-10-01T10:15:08.230 回答
1

As long as I know about mysql query will be quit tricky and buggy if some how you manage to write the one. I suggest go for cursor, here is the syntax of the cursor, here is the syntax of the cursor

于 2012-10-01T10:23:49.393 回答
1

Try this solution -

SELECT date FROM (
  SELECT
    date, @d := IF(@d IS NULL OR DATEDIFF(@d, date) > 1, date, @d) start_date
  FROM
    dates,
   (SELECT @d:=null) t
  ORDER BY
    date DESC
) t
WHERE start_date = date

The subquery finds out start days (18, 13, 11...), then WHERE condition filters records. Try to run the subquery to understand how it works -

  SELECT
    date, @d := IF(@d IS NULL OR DATEDIFF(@d, date) > 1, date, @d) start_date
  FROM
    dates,
   (SELECT @d:=null) t
  ORDER BY
    date DESC
于 2012-10-09T05:41:04.920 回答