您只需要在where
子句中添加逻辑即可:
SELECT `p_Id`,`user_id`,`doc_id`,`credit` ,`app_date`,`expires_on`,
(credit -debited_amount) AS credit
FROM `wp_loyalty_credits`
WHERE `expires_on`>Now() and (credit > debited_amount or debited_amount is null);
您的查询credit
在中重新定义select
。但是,这无关紧要,因为您不能在where
子句中引用列别名。因此,该列credit
就是它所使用的。如果添加表别名,则更加清楚:
SELECT lc.p_Id, lc.user_id, lc.doc_id, lc.credit, lc.app_date, lc.expires_on,
(lc.credit - lc.debited_amount) AS credit
FROM `wp_loyalty_credits` lc
WHERE lc.expires_on > Now() and
(lc.credit > lc.debited_amount or lc.debited_amount is null);