我想检索与我们合作超过 365 天的客户总数,这是我使用的查询字符串,它不起作用。
SELECT count(id) as total_customers
, DATEDIFF(lastvisit,firstvisit) AS Duration
from `customers`
having Duration >= 365
感谢您的帮助!
你应该把条件放到一个 WHERE 子句中:
SELECT count(*) as total_customers
FROM customers
WHERE DATEDIFF(lastvisit,firstvisit) >= 365
但我宁愿把它写成:
SELECT count(*) as total_customers
FROM customers
WHERE lastvisit >= firstvisit + INTERVAL 365 DAY
或者干脆说"一年"而不是"365天":
SELECT count(*) as total_customers
FROM customers
WHERE lastvisit >= firstvisit + INTERVAL 1 YEAR