我正在尝试使用NOW() - INTERVAL
运行一个日期列在很多天/月之前的查询,但是天数/月的数量在另一个称为payment_period
的列中
SELECT *
FROM customer_subscriptions
WHERE
status = 'Active' AND (
( auto_renew = '1' AND expiry_date <= '".date("Y-m-d")."')
OR (last_charge NOW() - INTERVAL payment_period)
)
答案 0 :(得分:2)
last_charge NOW()-间隔期付款
您缺少两件事:
假设付款期限表示为天,则需要:
last_charge >= NOW() - INTERVAL payment_period DAY
另一种可能性是将unit
存储在另一列中,然后:
last_charge >= CASE
WHEN unit = 'DAY' THEN NOW() - INTERVAL val DAY
WHEN unit = 'MONTH' THEN NOW() - INTERVAL val MONTH
-- ... other supported units ...
END