我想根據最大時間戳爲每個客戶在MySQL中選擇過去三天的行,但我不確定如何執行此操作。mysql爲每個客戶選擇過去3天的行
示例表:
id | timestamp | cust_ID
899900 | 2016-04-09 12:00:00 | 500219
899901 | 2016-04-12 16:00:00 | 500219
899902 | 2016-04-14 11:00:00 | 500219
899903 | 2016-04-15 12:00:00 | 500219
909901 | 2016-04-08 16:00:00 | 500230
909902 | 2016-04-14 11:00:00 | 500230
909903 | 2016-04-15 12:00:00 | 500230
939905 | 2016-05-02 19:00:00 | 500240
到目前爲止,我有以下幾點:
SELECT *
FROM table
WHERE timestamp BETWEEN max(timestamp) AND (max(timestamp) - INTERVAL 3 DAY)
GROUP BY cust_ID
但它給人的錯誤:
1111 'invalid use of group function'
順便說一下,在sql land中,2不是'3'和1之間。 – Strawberry