My understanding of left outer join is,
table1:
id(pk) name(unique_key) address phone
table2:
new_id(pk) name(foreign key) company work-experience
1st table:
1 a x 123
2 b y 234
3 c z 345
2nd table
1 a aaa 2
2 a aab 3
3 b aab 3
if I will do,
select * from table1 left outer join table2 on table1.name=table2.name,
it will give me
1 a x 123 1 a aaa 2
1 a x 123 2 a aab 3
2 b y 345 3 b aab 3
3 c z 345 NULL NULL NULL NULL
Now instead of above result, I want to get all the rows where company is aab. Also , for any entry in 1st table, if there is no corresponding entry in 2nd table then it should give me NULL for all columns in 2nd table.
like this:
1 a x 123 aab 3
2 b y 234 aab 3
3 c z 345 NULL NULL
is the above result possible with left outer join ? If not, How can I get the above result ?
You can simply add the conditions for the second table (right-side table), in the ON
part of LEFT JOIN
.
SELECT * FROM table1 AS t1
LEFT OUTER JOIN table2 AS t2
ON t1.name = t2.name AND
t2.company = 'aab'
Also, in case of multi-table queries, it is advisable to use Aliasing , for code clarity (enhanced readability), and avoiding unambiguous behaviour.
select t1.name,t1.address,t1.phoneNo,t2.comapnay,t2.workExperiance from table1 as t1 left outer join table2 as t2 on t1.name=t2.name AND t2.company = 'aab'
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.