Click here to view and discuss this page in DocCommentXchange. In the future, you will be sent there automatically.

SQL Anywhere 11.0.1 (中文) » SQL Anywhere 服务器 - SQL 的用法 » 查询和修改数据 » 连接:从多个表检索数据 » 自然连接

 

自然连接与 ON 子句一起使用

如果您指定了 NATURAL JOIN 并将连接条件置于 ON 子句中,则结果将是这两个连接条件的联合。

例如,以下两个查询是等效的。在第一个查询中,SQL Anywhere 生成连接条件 Employees.DepartmentID = Departments.DepartmentID。该查询还包含一个显式连接条件。

SELECT GivenName, Surname, DepartmentName
FROM Employees NATURAL JOIN Departments
  ON Employees.ManagerID = Departments.DepartmentHeadID;

下一个查询是等效的。在该查询中,上一个查询中生成的自然连接条件是在 ON 子句中指定。

SELECT GivenName, Surname, DepartmentName
FROM Employees JOIN Departments
  ON Employees.ManagerID = Departments.DepartmentHeadID
   AND Employees.DepartmentID = Departments.DepartmentID;