自然连接和内部连接有什么区别?
INNER JOIN 和 NATURAL JOIN 之间的一个显着区别是返回的列数。
考虑:
TableA TableB +------------+----------+ +--------------------+ |Column1 | Column2 | |Column1 | Column3 | +-----------------------+ +--------------------+ | 1 | 2 | | 1 | 3 | +------------+----------+ +---------+----------+
Column1 上的INNER JOINTableA 和 TableB 将返回
INNER JOIN
SELECT * FROM TableA AS a INNER JOIN TableB AS b USING (Column1); SELECT * FROM TableA AS a INNER JOIN TableB AS b ON a.Column1 = b.Column1; +------------+-----------+---------------------+ | a.Column1 | a.Column2 | b.Column1| b.Column3| +------------------------+---------------------+ | 1 | 2 | 1 | 3 | +------------+-----------+----------+----------+
Column1 上的NATURAL JOINTableA 和 TableB 将返回:
NATURAL JOIN
SELECT * FROM TableA NATURAL JOIN TableB +------------+----------+----------+ |Column1 | Column2 | Column3 | +-----------------------+----------+ | 1 | 2 | 3 | +------------+----------+----------+
避免了重复的列。
(来自标准语法的 AFAICT,您不能在自然连接中指定连接列;连接严格基于名称。另请参见Wikipedia。)
( 内部连接输出中有一个作弊;a.andb.部分不会出现在列名中;您只需将column1, column2, column1,column3作为标题。)
a.
b.
column1
column2
column3