从两个不同的表中选择值-sql

Selecting values from two different tables-sql

我有两个 tables A 和 B,具有相同的列 ID、日期、Main_id、数量。 Main_id 是 table 的相似列。我想把select ID, Date, a.quantity, b.quantity 改成table c.

答:

id |  date         |  main_id   | quantity_in
----------------------------------------------
12   4/10/2019      1                60
20   7/4/2019        2                30 
33    9/6/2019       3                10

乙:

id |  date         |  main_id   | quantity_out
----------------------------------------------
20   5/10/2019          1               10
40   7/4/2019           1               30 
53    9/6/2019          1               10

C: 结果table

id   |  date            |  main_id   | quantity_In  |   quantity_out
------------------------------------------------------------------
12    4/10/2019              1                60            null
20    5/10/2019              1               null            10
40    7/4/2019               1               null            30 
53    9/6/2019               1               null            10

这看起来像 union all:

select id, date, main_id, quantity_in, null quantity_out from a where main_id = 1
union all
select id, date, main_id, null, quantity_out from b where main_id = 1

你似乎想要union all:

select a.id, a.date, a.main_id, a.quantity_in, null as quantity_out
from a
where a.main_id = 1
union all
select b.id, b.date, b.main_id, null as quantity_in, b.quantity_out
from b
where b.main_id = 1;