2016-11-21 114 views
0

我不知道這是否可能,但這裏是我正在尋找的Mysql - 在記錄之前N天創建的最近記錄

我需要當前日期,如果創建的最後一條記錄超過N天或創建的第一條記錄的created_at日期之後N天內創建的第一條記錄的created_at

因此,如果存在兩條記錄,其創建日期爲差異大於N天,並且在這兩個日期之間不會創建其他記錄,請爲我提供最近創建的這些記錄的created_at,除非最近的created_at中的所有記錄都大於N天前,在這種情況下,請提供今天的da TE代替

所以對於這個表和N = 5

+----+-------------+ 
|id |created_at | 
+----+-------------+ 
|9 |1 day ago | 
|8 |2 days ago | 
|7 |3 days ago | 
|6 |4 days ago | 
|5 |5 days ago | < not this date 
|4 |6 days ago | < get this date 
|3 |12 days ago | < not this date 
|2 |13 days ago | < not this date 
|1 |20 days ago | 
+----+-------------+ 

我需要得到6 days ago但不13 days ago5 days ago,如果記錄看起來像這樣

+----+-------------+ 
|id |created_at | 
+----+-------------+ 
|5 |11 day ago | < not this date, instead today's date 
|4 |12 days ago | 
|3 |20 days ago | 
|2 |21 days ago | 
|1 |30 days ago | 
+----+-------------+ 

我需要今天的日期

+0

是'created_at '約會或字面意思是「11天前」? – WillardSolutions

+0

這是一個日期,我只是覺得X天前更容易閱讀的例子 – Jarfis

+0

如果日期是1,2,5,6,7,15天前,你想要15天前,因爲差距必須更大比N天多? – Barmar

回答

1

以查詢計算相鄰日期之間的差距開始:

SELECT created_at, DATEDIFF(@prevdate, created_at) AS diff, @prevdate := created_at 
FROM (SELECT created_at 
     FROM yourTable 
     WHERE created_at <= DATE_SUB(CURDATE(), INTERVAL @N DAY) 
     ORDER BY created_at DESC) AS x, 
CROSS JOIN (SELECT @prevdate := NULL) as vars 

然後添加了行,其中diff超過N的過濾器,並選擇最近的這些

SELECT MAX(created_at) AS selected_date 
FROM (SELECT created_at, DATEDIFF(@prevdate, created_at) AS diff, @prevdate := created_at 
     FROM (SELECT created_at 
      FROM yourTable 
      WHERE created_at <= DATE_SUB(CURDATE(), INTERVAL @N DAY) 
      ORDER BY created_at DESC) AS x 
     CROSS JOIN (SELECT @prevdate := NULL) as vars) AS z 
WHERE diff > @N; 

最後,爲了獲得當前的日期,如果最近的日期超過N天以前,您可以使用另一個查詢獲取最新日期,並使用IF()

SELECT IF (latest_date < DATE_SUB(CURDATE(), INTERVAL @N DAY), CURDATE(), MAX(created_at)) AS selected_date 
FROM (SELECT created_at, DATEDIFF(@prevdate, created_at) AS diff, @prevdate := created_at 
    FROM (SELECT created_at 
      FROM yourTable 
      WHERE created_at <= DATE_SUB(CURDATE(), INTERVAL @N DAY) 
      ORDER BY created_at DESC) AS x 
    CROSS JOIN (SELECT @prevdate := NULL) as vars) AS z 
CROSS JOIN (SELECT MAX(created_at) AS latest_date FROM yourTable) AS y 
WHERE diff > @N; 

DEMO

+0

我不認爲這是我正在尋找的,不會只是返回比N天之前的最新記錄更新的第一條記錄的created_at? – Jarfis

+1

這就是你要求的,但看起來你自從我發佈以來就改變了這個問題。 – Barmar

+0

對不起,我在示例中的第一遍還不夠詳細 – Jarfis

0

我去

SELECT IF (latest_date < DATE_SUB(CURDATE(), INTERVAL @N DAY), CURDATE(), MAX(fetch_prevdate)) AS selected_date 
FROM (SELECT created_at, @prevdate as fetch_prevdate, DATEDIFF(@prevdate, created_at) AS diff, @prevdate := created_at 
     FROM (SELECT created_at 
      FROM yourTable 
      WHERE created_at <= DATE_SUB(CURDATE(), INTERVAL @N DAY) 
      ORDER BY created_at DESC) AS x 
     CROSS JOIN (SELECT @prevdate := NULL) as vars) AS z 
CROSS JOIN (SELECT MAX(created_at) AS latest_date FROM yourTable) AS y 
WHERE diff > @N; 

Barmar的回答是一樣的,我需要什麼,但我有一個字段添加到的時間差子查詢之前存儲@prevdate值99%它被重新分配在select中並且得到MAX而不是created_at字段