我想从两个表格的保单和付款中获取余额的值。下面的MySQL代码:
SELECT Sum(policy.premium) AS
`total`
,
(SELECT Sum(payments.amount)
FROM payments
WHERE ( payments.date_paid BETWEEN '2019-03-01' AND '2019-03-31' )) AS
`paid`
FROM `policy`
LEFT JOIN payments
ON policy.code = payments.code
WHERE ( policy.st BETWEEN '2019-03-01' AND '2019-03-31' )
AND policy.trn_type = 0
已付费列返回null,如何获得总和已付费之间的差额。
答案 0 :(得分:0)
付费栏仅是这部分:
SELECT SUM(payments.amount)
FROM payments
WHERE payments.date_paid BETWEEN '2019-03-01' AND '2019-03-31'
如果返回null,请先检查数据中的内容
答案 1 :(得分:0)
您可以像这样计算两个值:
select t.total, coalesce(t.paid, 0) paid, (t.total - coalesce(t.paid, 0)) diff from (
select
(select sum(premium) from policy where st between '2019-03-01' and '2019-03-31' and policy.trn_type=0) total,
(select sum(amount) from payments where date_paid between '2019-03-01' and '2019-03-31') paid
) t