Zero皇的結果會得到以下錯誤的結果
我只會用子查詢的方式
SELECT *,
Datediff(receivedate, orderdate) AS diffdate
FROM order_table
WHERE Datediff(receivedate, orderdate) = (SELECT Max(Datediff(receivedate, orderdate))
FROM order_table)
用MAX()函數去取最大值,DATEDIFF()取日期相減值,最後由大到小排序第1筆。
SELECT order_id, MAX(DATEDIFF(receivedate,orderdate)) AS diffDate FROM order_table
GROUP BY order_id
ORDER BY diffDate DESC Limit 1;
(之前錯誤的答案):SELECT order_id, MAX(DATEDIFF(receivedate,orderdate)) FROM order_table;