MySQL 三表自然连接怎么做?

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

How to do 3-table Natural Join?

mysqlsql

提问by Yunus Eren Güzel

Is this the correct way of writing three natural joins? :

这是编写三个自然连接的正确方法吗?:

SELECT C.name, P.name 
FROM client C NATURAL JOIN order O NATURAL JOIN product P 
WHERE O.date > '15.02.2011'

回答by LBushkin

This is indeed the typical syntax for natural joins.However, not all databases support natural joins (for instance, I don't believe SQLServer supports it) and I don't believe there is an ANSI standard for natural join.

这确实是自然连接的典型语法。但是,并非所有数据库都支持自然连接(例如,我不相信 SQLServer 支持它)并且我不相信有自然连接的 ANSI 标准。

Note that natural joinsare generally considered dangerous and something to be avoided - this is because they obscure the join relationship that a query depends on, and could result in queries whose meaning changes if the data model is altered.

请注意,自然连接通常被认为是危险的并且需要避免——这是因为它们掩盖了查询所依赖的连接关系,并且如果数据模型被更改,可能会导致查询的含义发生变化。

回答by onedaywhen

To check syntax when your SQL product of choice does not support it, use the Mimer SQL-92 validator. You should discover that orderand dateare reserved words. Change them to my_orderand my_daterespectively and you will then discover yours is valid Transitional SQL-92 syntax.

要在您选择的 SQL 产品不支持语法时检查语法,请使用Mimer SQL-92 验证器。你应该发现orderdate是保留字。将它们分别更改为my_ordermy_date,然后您会发现您的是有效的 Transitional SQL-92 语法。

回答by Marcin Krupowicz

using this syntax is considered dangerous as it was already mentioned. Consider the following example: T1(id, name, age) T2(id, age, city)

使用这种语法被认为是危险的,因为它已经提到过。考虑以下示例:T1(id, name, age) T2(id, age, city)

SELECT T1.id, T1.name, T1.age, T2.age
FROM T1 NATURAL JOIN T2

Which column should be used for join? id or age? It is not defined. Every vendor can implement his own way to solving such problems.

哪个列应该用于连接?身或年龄?它没有定义。每个供应商都可以采用自己的方式来解决此类问题。

Instead of this syntax, consider using traditional join syntax:

考虑使用传统的连接语法,而不是这种语法:

SELECT T1.....
FROM T1 INNER JOIN T2 on T1.id=T2.id