任务付款
SELECT t.id AS task_id, t.name, t.created_at
,COALESCE(SUM(tp1.amount),0) AS paid
,COALESCE(SUM(tp2.amount),0) AS paid_back
FROM tasks AS t
LEFT JOIN task_payments AS tp1 ON tp1.task_id=t.id AND tp1.type='1'
LEFT JOIN task_payments AS tp2 ON tp2.task_id=t.id AND tp2.type='0'
WHERE t.customer_id='4'
GROUP BY tp1.task_id, tp2.task_id
ORDER BY t.id ASC
嗨,task_payments 上有两种类型(1 或 0)。类型 0 已偿还。类型 1 已付费。我想要单独的总金额作为结果。所以我想要结果;task_id=5paid=450paid_back=10 我应该使用join。如果有过滤请求,我将在 where 子句中使用paid 和paid_colums。例如:和paid_back>0