Check if a particular ID exists in another table or not using MySQL join

0
1302

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

Solution

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

LEAVE A REPLY

Please enter your comment!
Please enter your name here