简体   繁体   中英

Add condition while using LEFT OUTER JOIN

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

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.

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

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM