SQL 使用 3 列在 2 个表之间进行左连接并连接到 Oracle 上的第三个表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5529986/
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-09-01 10:00:43  来源:igfitidea点击:

Left join between 2 tables using 3 columns and joing to a third table on Oracle

sqloraclejoinleft-join

提问by Mr S

i am using Oracle and need to left join 2 tables (which are actually the same table with alias) based on 3 columns and then join with a third table. What should be the best syntax?

我正在使用 Oracle 并且需要基于 3 列离开连接 2 个表(实际上是具有别名的同一个表),然后与第三个表连接。最好的语法应该是什么?

Select table_3.column_x
  From (table_1 left join table_2
 Using (column_1 , column_2 , column_3)), table_3
 Where Table_2.column_1 = table_3.column_1

Should I use ‘,' on the ‘using statement or ‘AND'? Where exactly should I insert the table_3 statement even if it is not used on the left join?

我应该在“using 语句”还是“AND”上使用“,”?即使在左连接中没有使用 table_3 语句,我究竟应该在哪里插入它?

回答by Erkan Haspulat

Using ANSI SQL:

使用 ANSI SQL:

select * from
TABLE1 "TABLE1"
left join TABLE1 "TABLE2" on(TABLE1.c1 = TABLE2.C1 and TABLE1.c2 = TABLE2.C2)
left join TABLE3 "TABLE3" on(TABLE1.c3 = TABLE3.c3)

Using Oracle Join Syntax you have:

使用 Oracle 连接语法,您有:

select * from TABLE1 "TABLE1", TABLE1 "TABLE2", TABLE3 "TABLE3"
where
    TABLE1.c1 = TABLE2.c1 (+)
and TABLE1.c2 = TABLE2.c2 (+)
and TABLE1.c3 = TABLE3.c3 (+)

(+) here represents the left join. I personally prefer ANSI SQL way. It seems cleaner to me. Your join predicates might not be the same with my example, keep that in mind.

(+) 这里代表左连接。我个人更喜欢 ANSI SQL 方式。对我来说似乎更干净。您的连接谓词可能与我的示例不同,请记住这一点。