早上好,用这个把我的头撞在墙上,希望得到一些建议。我连接了 3 个表来显示销售数据,如下所示;
Select
a._CommMonth As Month,
Sum(a._CommDue) As Commission,
Sum(a.`Network Paid`) As Company,
Sum(a.Payable) As Adviser,
c.Fee As Charge,
Sum(a.Payable) - (c.Fee) As Total
From
pms_payments.dbo_payments a
Inner Join pms_payments.dbo_nscontacts b
On b._Firm_Name = a._Firm_Name
Left Join pms_payments.dbo_clawback c
On b.ContactID = c.ContactID And a._CommMonth = c.Month
Where
b.ContactID = 199
Group By
a._CommMonth
Sum(a.Payable) - (c.Fee) 由于 Total 字段是我遇到问题的地方,如果 c.fee 表中不存在任何记录,则不会返回任何内容,例如 100 - [No record] = 100。如果存在记录,则按预期扣除该值,例如 100 - 15 = 85。
这有意义吗?
谢谢
最佳答案
看看使用 IFNULL
IFNULL(expr1,expr2)
If expr1 is not NULL, IFNULL() returns expr1; otherwise it returns expr2. IFNULL() returns a numeric or string value, depending on the context in which it is used.
有点像
Sum(a.Payable) - IFNULL(c.Fee,0)
关于没有记录时MySql计算返回零,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13027417/