【发布时间】:2016-04-16 11:50:48
【问题描述】:
我有一个 sql 查询,它从三列返回一个表的总数,这是我的查询:
SELECT c.*,
(select sum(t.incoming) - sum(t.outgoing) from transactions t where t.client_id = c.id and currency = 'Dollar') +
(select sum(t.equal_to_dollar) from transactions t where t.incoming != 0 and currency != 'Dollar' and t.client_id = c.id) -
(select sum(t.equal_to_dollar) from transactions t where t.outgoing != 0 and currency != 'Dollar' and t.client_id = c.id)
as total from clients c
我的问题是,当第二个和第三个(内部选择)中的条件之一不满足时,它将返回空值并导致整个选择返回空值。因为空值不能与任何值相加或相减。
我想知道是否有任何方法可以将其条件评估为 false 的查询结果设置为零,这样它不会影响整个查询,并且查询将返回满足 where 子句中条件的那些部分的值。
我不知道当结果很重要并且应该如此准确时,这是一种计算价值的好方法吗?
我要提前感谢您的帮助:)
【问题讨论】:
标签: mysql