简体   繁体   English

在Oracle SQL Developer中联接表

[英]Joining Tables in Oracle SQL Developer

I have four tables that I want to join and display the output all together. 我有四个要联接的表,并一起显示输出。 I'm not sure how the syntax works for Oracle SQL Developer. 我不确定Oracle SQL Developer的语法如何工作。 I know this is an easy question for a programmer and I was hoping someone can make a suggestion on how the code might look like. 我知道这对程序员来说是一个简单的问题,我希望有人可以对代码的外观提出建议。

The tables are: 这些是:

JNL1
JNL2
JNL3
JNL4

The key that is common between all four of these tables is ItemID . 这四个表之间的通用键是ItemID

How would the query look like? 查询的外观如何? Thanks 谢谢

It really depends on what kind of join you want (outer or not) but you can use default SQL syntax. 它实际上取决于所需的联接类型(外部联接与否),但是您可以使用默认的SQL语法。

For example, joining without the JOIN keyword: 例如,不使用JOIN关键字JOIN

select * from JNL1, JNL2, JNL3, JNL4,
where 
JNL1.ItemID = JNL2.ItemID AND
JNL2.ItemID = JNL3.ItemID AND
JNL3.ItemID = JNL4.ItemID;

Additionally you can make use of multiple INNER JOINS eg 另外,您可以使用多个INNER JOINS例如

SELECT whatever
  FROM JNL1 AS a
INNER 
  JOIN JNL2 AS b
    ON b.ItemID = a.ItemID
INNER 
  JOIN JNL2 AS c
     ON c.ItemID = b.ItemID
INNER 
  JOIN JNL2 AS d
     ON d.ItemID = c.ItemID

It works in Oracle as it would in other DB engines : 它在Oracle中的工作方式与在其他数据库引擎中一样:

SELECT *
FROM JNL1 j1
INNER JOIN JNL2 j2 ON j1.ItemID = j2.ItemID
INNER JOIN JNL3 j3 ON j1.ItemID = j3.ItemID
INNER JOIN JNL4 j4 ON j1.ItemID = j4.ItemID

One typical Oracle syntax exists when you want to LEFT JOIN : 当您想LEFT JOIN时,存在一种典型的Oracle语法:

Standard SQL: 标准SQL:

SELECT *
FROM JNL1 j1
LEFT JOIN JNL2 j2 ON j1.ItemID = j2.ItemID
LEFT JOIN JNL3 j3 ON j1.ItemID = j3.ItemID
LEFT JOIN JNL4 j4 ON j1.ItemID = j4.ItemID

is equivalent to this Oracle syntax: 等效于以下Oracle语法:

SELECT *
FROM JNL1 j1,
JNL2 j2,
JNL3 j3,
JNL4 j4,
WHERE j1.ItemID=j2.ItemID(+)
AND j1.ItemID=j3.ItemID(+)
AND j1.ItemID=j4.ItemID(+)

If you want to output the rows from all the tables, and if the columns are the same in each table, UNION ALL will work. 如果要输出所有表中的行,并且每个表中的列均相同,则UNION ALL将起作用。 Note that this isn't a JOIN in the database sense, though it kind of is in an English sense: 请注意,这不是数据库意义上的JOIN ,尽管它是英文意义上的:

SELECT * FROM JNL1
UNION ALL SELECT * FROM JNL2
UNION ALL SELECT * FROM JNL3
UNION ALL SELECT * FROM JNL4

You can add an ORDER BY to the end if needed. 如果需要,可以在末尾添加ORDER BY

UNION is similar to UNION ALL , but it will omit rows where all of the values are duplicates. UNIONUNION ALL相似,但是它将忽略所有值都是重复的行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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