Tuesday 4 September 2018

Mysql Left join does not work as expected

I am using a simple Left Join in my query.But the problem query is not working as expected.

Query Is
select m.* from Menu m
left join MenuRole mr on m.Id=mr.MenuID
where mr.DesignationID=1

Menu Table
MenuRole Table
Output of query
The problem is the Employee data is missing from the result and the parentID columnwith Id=0 is also missing.

This is caused by the where clause, when threre is no match DesignationId is null and therefore DesignationId = 1 is not true.
Try this:
select m.* from Menu m
left join MenuRole mr on m.Id=mr.MenuID and mr.DesignationID=1

0 comments:

Post a Comment