我以这种方式请求过去 6 个月的客户:
SELECT customer
FROM table
WHERE date_of_last_order>date_sub(NOW(),INTERVAL 6 MONTH)
GROUP BY customer
ORDER BY customer ASC
Run Code Online (Sandbox Code Playgroud)
现在我还想要一份超过 6 个月的旧订单列表,但不应包括过去 6 个月的客户。
有任何想法吗?谢谢!
你想使用一个having子句:
SELECT customer
FROM table
GROUP BY customer
HAVING MAX(date_of_last_order) <= date_sub(NOW(), INTERVAL 6 MONTH)
ORDER BY customer ASC
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
59 次 |
| 最近记录: |