使用LEFT OUTER JOIN时添加条件

时间:2011-08-22 13:08:47

标签: sql postgresql

我有两个表我想在使用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

我只想要那些有可见= 1的记录是真的但查询忽略条件和一件事我必须要使用左外连接因为我想从左表记录甚至记录不存在于右表中如何检查条件是我只得到左表中那些可见为1的记录

2 个答案:

答案 0 :(得分:12)

试试这个

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

我将tt.visible = 1移到了where子句中。 vct.employee = 63需要保留在联接中,否则您将无法进行外部联接。

答案 1 :(得分:2)

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