我创建了一个查询,当从子查询返回 null 时我想返回 0,我使用了 IFNULL 函数,但最后我仍然得到 null。下面是一个类似的查询,请帮助,不知道该怎么做。 (PS:我是用手机写的,所以mu格式可能会出现问题,抱歉)
SELECT t1.id, t1.Amount, t1.parent,
( SELECT @Total := IFNULL(SUM(t2.amount), 0) AS amount FROM transactions t2 WHERE t1.`id` = t2.parent GROUP BY t2.parent ) AS AmountRepaid,
( SELECT CASE WHEN CAST(SUM(t3.amount) AS DECIMAL) > CAST(t1.amount AS DECIMAL) THEN "Over" WHEN CAST(SUM(t3.amount) AS DECIMAL) = CAST(t1.amount AS DECIMAL) THEN "Complete" ELSE "Incomplete" END FROM transactions t3 WHERE t1.`id` = t3.Parent GROUP BY t3.Parent ) AS PaymentStatus
FROM transactions t1 WHERE t1.`Parent` IS NULL;
最佳答案
尝试使用以下方式:
SELECT t1.id, t1.Amount, t1.parent,
@Total := IFNULL( ( SELECT SUM(t2.amount) AS amount FROM transactions t2 WHERE t1.`id` = t2.parent GROUP BY t2.parent ), 0) AS AmountRepaid,
关于mysql - 如何从 mysql 中的子查询返回零而不是 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24039550/