Joining 2 SQL SELECT result sets into one

2019-03-11 18:08发布

I've got 2 select statements, returning data like this:

Select 1 col_a col_b

Select 2 col_a col_c

If I do union, I get something like col_a col_b

And rows joined. What i need is getting it like this: col_a col_b col_c

Joined on data in col_a.

3条回答
我只想做你的唯一
2楼-- · 2019-03-11 18:10

Use JOIN to join the subqueries and use ON to say where the rows from each subquery must match:

SELECT T1.col_a, T1.col_b, T2.col_c
FROM (SELECT col_a, col_b, ...etc...) AS T1
JOIN (SELECT col_a, col_c, ...etc...) AS T2
ON T1.col_a = T2.col_a

If there are some values of col_a that are in T1 but not in T2, you can use a LEFT OUTER JOIN instead.

查看更多
老娘就宠你
3楼-- · 2019-03-11 18:10
SELECT table1.col_a, table1.col_b, table2.col_c 
  FROM table1 
  INNER JOIN table2 ON table1.col_a = table2.col_a
查看更多
够拽才男人
4楼-- · 2019-03-11 18:29

Use a FULL OUTER JOIN:

select 
   a.col_a,
   a.col_b,
   b.col_c
from
   (select col_a,col_bfrom tab1) a
join 
   (select col_a,col_cfrom tab2) b 
on a.col_a= b.col_a
查看更多
登录 后发表回答