我想知道在两个表(或多个表)之间创建连接的最佳方法是什么。 我见过两种不同的方法。
在下一个示例中,我有2个表。 表1具有customerId(键)和CustomerName。 表2具有customerId(密钥)和CustomerPhone
表1:
customerId | customerName|
============+=============+
1 | Josh |
2 | Nadia |
表2:
customerId | customerPhone|
============+==============+
1 | 123 |
2 | 456 |
哪一个查询是最好的以及为什么:
SELECT Table1.customerId, Table2.customerPhone
FROM Table1, Table2
WHERE Table1.customerId = Table2.customerId
查询2:
SELECT Table1.customerId, Table2.customerPhone
FROM Table1
Inner Join Table2 ON Table1.customerId = Table2.customerId
第二种选择更常见,也被认为是正确的选择,然而它们都在做着相同的事情
第一个查询首先进行交叉连接,然后减少结果以适应where CFLUUSE。
第二种方法通过检查ON子句ist中的条件是否满足来匹配表。
两者都是一样的,但第一个是slowe
第一个查询使用旧语法(SQL-89):
SELECT Table1.customerId, Table2.customerPhone
FROM Table1, Table2
WHERE Table1.customerId = Table2.customerId
第二个查询是用现代语法(SQL-92)编写的:
SELECT Table1.customerId, Table2.customerPhone
FROM Table1
Inner Join Table2 ON Table1.customerId = Table2.customerId
它们是等价的。 请使用SQL-92。 使用SQL-92!