【问题标题】:Selecting values from two different tables-sql从两个不同的表中选择值-sql
【发布时间】:2020-10-20 12:15:11
【问题描述】:

我有两个表 A 和 B 具有相同的列 ID、日期、Main_id、数量。 Main_id 是两个表的类似列。我想在表c中选择ID、Date、a.quantity、b.quantity。

答:

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:结果表

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。

标签: sql join select union oracle12c


【解决方案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;

【讨论】:

    【解决方案2】:

    这看起来像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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-03-16
      • 1970-01-01
      • 2017-01-26
      • 1970-01-01
      • 1970-01-01
      • 2017-02-20
      相关资源
      最近更新 更多