Sql Oracle使用USE?

huangapple go评论102阅读模式
英文:

Sql Oracle USING USE?

问题

我正在尝试使用"USING"而不是"ON"来进行查询。

问题在于在"Employees"表中它被称为"codigo",而在"trabajan"表中它被称为"cod_emp"。

使用"USING"如何连接这两个表?
根据Oracle文档,这是可能的,但我无法实现。

以下是Oracle文档中的示例:
https://docs.oracle.com/javadb/10.6.2.1/ref/rrefsqljusing.html

下面的查询与上面的查询类似,但有附加的连接条件,即COUNTRIES.COUNTRY_ISO_CODE等于CITIES.COUNTRY_ISO_CODE:

SELECT * FROM COUNTRIES JOIN CITIES
USING (COUNTRY, COUNTRY_ISO_CODE)

英文:

I am trying to use the "USING" instead of "ON" to make a query.

The problem is that in the "Employees" table it is called "codigo" and in the "trabajan" table it is called "cod_emp".

With the USING how would you join these two tables?
According to the Oracle documentation it is possible, but I can't get it.

SELECT NOMBRE, FUNCION
FROM EMPLEADOS
JOIN TRABAJAN USING (CODIGO,COD_EMP);

The example from the oracle documentation:
https://docs.oracle.com/javadb/10.6.2.1/ref/rrefsqljusing.html

The next query is similar to the one above, but it has the additional join condition that COUNTRIES.COUNTRY_ISO_CODE is equal to CITIES.COUNTRY_ISO_CODE:

SELECT * FROM COUNTRIES JOIN CITIES
    USING (COUNTRY, COUNTRY_ISO_CODE)

答案1

得分: 2

为什么你不应该在Oracle中使用"USE"。一个快速的抱怨。

  • 当列名不匹配时无法使用
  • 是特定于Oracle且不可移植的
  • 完全没有附加值,因为它纯粹是语法糖。
  • 由于它是语法糖,很难与使用标准语法的其他情况进行比较。

简而言之,永远不要使用自然连接。

英文:

Why you shouldn't use "USE" in oracle. A quick rant.

  • Can't use it when column names don't match
  • Is specific to Oracle and non-portable
  • Adds absolutely no value as it is purely syntax sugar.
  • Since it is syntax sugar it is hard to compare to other cases where standard syntax is used.

In short, don't use natural joins ever.

答案2

得分: 2

以下是您要翻译的内容:

> 当您指定具有相同名称的列的等值连接时,使用列子句表示要使用的列。**只有在两个表中的连接列具有相同名称时,才能使用此子句。**在此子句中,请不要使用表名或表别名限定列名。

如果您确实需要在一个子查询中对其中一个列名进行别名,然后使用USING进行分配,可以这样做:

SELECT NOMBRE, FUNCION
FROM (
  SELECT CODIGO AS COD_EMP, NOMBRE FROM EMPLEADOS
)
JOIN TRABAJAN USING (COD_EMP);

...这似乎不是非常“方便”。更简单的方法是使用ON来处理名称不匹配的情况:

SELECT NOMBRE, FUNCION
FROM EMPLEADOS
JOIN TRABAJAN ON CODIGO = COD_EMP;

fiddle

还有一个好主意是使用表名或别名限定所有列名,以便您可以看到每个列来自哪个表,您可以使用表别名来使其更短,例如:

SELECT E.NOMBRE, T.FUNCION
FROM EMPLEADOS E
JOIN TRABAJAN T ON E.CODIGO = T.COD_EMP;

(但您不要使用USING限定公共列,个人认为这会让人感到困惑,这也是避免使用该形式的连接语法的另一个原因。)

英文:

From the Oracle 19c documentation, not the JavaDB docs you linked to, and with emphasis added:

> When you are specifying an equijoin of columns that have the same name in both tables, the USING column clause indicates the columns to be used. You can use this clause only if the join columns in both tables have the same name. Within this clause, do not qualify the column name with a table name or table alias.

I you really had to use USING for an assignment then you could do so if you alias one of the column names in a subquery:

SELECT NOMBRE, FUNCION
FROM (
  SELECT CODIGO AS COD_EMP, NOMBRE FROM EMPLEADOS
)
JOIN TRABAJAN USING (COD_EMP);

... which doesn't seem very "comfortable". It's simpler to use ON to handle the name discrepancy:

SELECT NOMBRE, FUNCION
FROM EMPLEADOS
JOIN TRABAJAN ON CODIGO = COD_EMP;

fiddle

It's also a good idea to qualify all of the column names with the table names or aliases so you can see where each column is coming from, and you can use table aliases to make that shorter, e.g.

SELECT E.NOMBRE, T.FUNCION
FROM EMPLEADOS E
JOIN TRABAJAN T ON E.CODIGO = T.COD_EMP;

(But you don't qualify common columns with USING, which personally I find confusing and another reason to avoid that form of join syntax.)

答案3

得分: 0

我会避免使用这种连接定义。我更喜欢像下面这样写:

SELECT NOMBRE, FUNCION
FROM EMPLEADOS e
JOIN TRABAJAN t on e.CODIGO=t.COD_EMP;

这样更清晰,也允许更复杂的连接条件。还请注意别名的使用,在选择语句中如果有两个或更多不同表中具有相同名称的列时,这非常重要。我从未使用过这种连接方式或所谓的自然连接,也从未觉得需要使用。

英文:

I'd avoid such join definitions.
I'd rather write like below:

SELECT NOMBRE, FUNCION
FROM EMPLEADOS e
JOIN TRABAJAN t on e.CODIGO=t.COD_EMP;

This is more clear and allows for more complex join conditions. Notice also the usage of aliases, which is very important in case you have columns with the same name in two or more different tables in the select statement. I have never used that way of writing the joins or the so-called natural join and never felt I would need to.

huangapple
  • 本文由 发表于 2023年5月17日 23:08:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/76273570.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定