SELECT product_key.contact_email, product_key.client_name, product_key.status, product_key.key, payment.paymentdate, product_key.id, MAX(paymentdate) AS latest_payment, DATE_ADD(MAX(paymentdate), INTERVAL 1 MONTH) AS expiration_date
FROM product_key LEFT OUTER JOIN payment ON payment.keyid=product_key.id
WHERE product_key.status = 'purchased' AND expiration_date = DATE_ADD(NOW(), INTERVAL 10 DAY) GROUP BY product_key.id
ORDER BY client_name asc
这是我的查询。我知道我不能在 WHERE 子句中使用别名,因为在 SELECT 之前首先读取了 WHERE。但即使我使用这样的东西:
SELECT product_key.client_name, DATE_ADD(MAX(paymentdate), INTERVAL 1 MONTH) AS expiration_date
FROM product_key LEFT OUTER JOIN payment ON payment.keyid=product_key.id
WHERE DATE_ADD(MAX(paymentdate), INTERVAL 1 MONTH) = DATE_ADD(NOW(), INTERVAL 10 DAY) AND product_key.status = 'purchased'
GROUP BY product_key.id
ORDER BY client_name asc
还是一个错误。请帮忙。谢谢。