1 需求:某人借钱(A表),还钱(B表) 要查询这个人还欠多少钱?,1次借钱可能对应多次还钱 (1-- 0.....n)
方法1
以上是错误的
方法1
select a.app_id, sum(a.借钱), sum(b.还钱)
from A
left join on B on a.id_A = b.id_A
group by a.id_App
以上是错误的
select a1, a2, sum(借钱), max(常量), sum(还钱)
from (select A.a1 as a1, A.a2 as a2, 借钱 as 借钱, '', 0 as 还钱
from A
union all
select A.a1 as a1, A.a2 as a2, 0 as 借钱, b.常量, b.还钱 as 还钱
from A, B
where A.id_A = B.id_A) C