0
我試圖使用從該查詢SO質疑Check for x consecutive days - given timestamps in database計算大量的連續天用戶提交的活性,即3天,5天,7天等獲得連續3天
查詢是:
SELECT IF(COUNT(1) > 0, 1, 0) AS has_consec
FROM
(
SELECT *
FROM
(
SELECT IF(b.dateAdded IS NULL, @val:[email protected]+1, @val) AS consec_set
FROM activity a
CROSS JOIN (SELECT @val:=0) var_init
LEFT JOIN activity b ON
a.userID = b.userID AND
a.dateAdded = b.dateAdded + INTERVAL 1 DAY
WHERE a.userID = 1
) a
GROUP BY a.consec_set
HAVING COUNT(1) >= 3
) a
當日期字段不是dateTime但代碼如何修改代碼以忽略dateTime的時間組件?我曾嘗試使用DATE(dateAdded),但沒有奏效。
我的數據是這樣的:
userID dateAdded
1 2016-07-01 17:01:56
1 2016-07-02 12:45:49
1 2016-07-03 13:06:27
1 2016-07-04 12:51:10
1 2016-07-05 15:51:10
2 2016-07-06 16:51:10
2 2016-07-07 11:51:10
1 2016-07-08 11:26:38
感謝
嘗試此http: //www.tutorialspoint.com/mysql/mysql-date-time-functions.htm#function_timestampadd –
嘗試將您的日期時間字段設置爲日期,就像這個'DATE(a.dateAdded)' – 1000111
試過了,它不起作用。如果我將時間組件更改爲相同的查詢工作,但即使投射到DATE也沒有產生正確的結果。 – puks1978