postgresql 在使用 LEFT OUTER JOIN 时添加条件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7147943/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-20 23:13:47  来源:igfitidea点击:

Add condition while using LEFT OUTER JOIN

sqlpostgresql

提问by Deepak Kumar

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

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

我只想要那些可见的记录 = 1 是真的,但查询忽略条件,还有一件事我必须使用左外连接因为我想要从左表中记录,即使右表中不存在记录如何检查条件我将只从左表中获取可见为 1 的那些记录

回答by Mikael Eriksson

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 = 1to the where clause instead. vct.employee = 63need to stay in the join because otherwise you would not have an outer join.

tt.visible = 1改为使用 where 子句。vct.employee = 63需要留在连接中,否则您将没有外部连接。

回答by StevieG

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