如何解决从两个不同的表中选择值-SQL
我有两个表A和B,它们具有相同的ID,日期,Main_id,数量列。 Main_id是两个表的相似列。我想在表c中选择ID,日期,a.quantity和b.quantity。
A:
id | date | main_id | quantity_in
----------------------------------------------
12 4/10/2019 1 60
20 7/4/2019 2 30
33 9/6/2019 3 10
B:
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
:
select id,date,main_id,quantity_in,null quantity_out from a where main_id = 1
union all
select 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;
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。