(My)SQL full join with three tables

前端 未结 3 1871
粉色の甜心
粉色の甜心 2020-12-19 11:01

I have tree tables

ID    A
-----------
1     10

ID    B
-----------
1     20
2     30

ID    C
-----------
2     40
3     50

Can anybody p

3条回答
  •  星月不相逢
    2020-12-19 11:48

    select coalesce(a.Id, b.Id, c.Id) ID, 
           case when a.A > 0 then a.A else 0 end, 
           case when b.B > 0 then b.B else 0 end,
           case when c.C > 0 then c.C else 0 end,
           ifnull(a.A, 0) + ifnull(b2.B, 0) - ifnull(c.C, 0) R
     from tableA a
     right outer join tableB b on a.id = b.id
     right outer join tableC c on b.id = c.id;
    

提交回复
热议问题