您好,我有交易表。 BigDecimal 金额和 bool 结果。 如果结果为真,我就买了一些东西,如果结果为假,我就卖掉了一些东西。 当我做这样的事情时,我想在单个查询中获取金额
SELECT (amount(when outcome is false) - amount(when outcome is true)) AS income
FROM transaction WHERE (the condition I was unable to write);
有没有办法在单个查询中做到这一点?
最佳答案
我认为你只想要条件聚合:
SELECT SUM(CASE WHEN outcome THEN - amount ELSE amount END) as income
FROM transaction;
关于mysql - 一张表的SQL算术运算,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49253569/