Reputation: 89
I have two SQL tables, and I like to have the result shown below.
table_1
id userId
---------------
1 u1
2 u2
3 u3
4 u4
5 u5
table_2
userId flag
-----------------
u1 1
u4 0
Result I need
id userId flag
------------------------
1 u1 1
2 u2 Null
3 u3 Null
4 u4 0
5 u5 Null
Upvotes: 1
Views: 6283
Reputation: 69440
use the left outer join:
select table_1.id, table_1.userid, table_2.flag
from table_1
left outer join table_2 on table_1.userid=table_2.userid
Upvotes: 1
Reputation: 153
you can use join or Under-expression for join :
select
t1.id,
t1.userid,
t2.flag
from table_1 t1
inner join table_2 t2
on t1.userid = t2.userid
under-expression
select
t1.id,
t1.userid,
(select flag from table_2 t2 WHERE t1.userid=t2.userid) as flag
from table_1 t1
Upvotes: 0
Reputation: 2306
You can use this following LEFT JOIN for the purpose, It will give all right table data with matched right table data .
SELECT * FROM table_1 LEFT JOIN table_2 on table_1.userId=table_2.userId
Upvotes: 2