Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add condition while using LEFT OUTER JOIN

Tags:

sql

postgresql

I have two table i want to add one condition while using LEFT OUTER JOIN

select tt.description,tt.visible,vct.currvalue 
from tblemployee tt 
left outer join viwcurrentemployee vct on vct.transitiontype = tt.cid
                                      and vct.employee = 63 
                                      and tt.visible = 1 
order by tt.cid

i want only those record which is have visible = 1 that is true but query ignore the condition and one thing i must have to use left outer join coz i want record from left table even record not present in right table how to check the condition that i will get only those record from left table in which visible is 1

like image 723
Deepak Kumar Avatar asked Dec 05 '22 20:12

Deepak Kumar


2 Answers

Try this

select tt.description,
       tt.visible,
       vct.currvalue 
from tblemployee tt 
  left outer join viwcurrentemployee vct 
    on vct.transitiontype = tt.cid and 
       vct.employee = 63
where tt.visible = 1 
order by tt.cid

I moved tt.visible = 1 to the where clause instead. vct.employee = 63 need to stay in the join because otherwise you would not have an outer join.

like image 133
Mikael Eriksson Avatar answered Dec 08 '22 14:12

Mikael Eriksson


select tt.description,tt.visible,vct.currvalue 
from tblemployee tt 
left outer join viwcurrentemployee vct on vct.transitiontype = tt.cid
                                      and vct.employee = 63 
where tt.visible = 1 
order by tt.cid
like image 30
StevieG Avatar answered Dec 08 '22 15:12

StevieG