INNER JOIN ON vs WHERE clause(INNER JOIN ON vs WHERE 子句)
问题描述
为简单起见,假设所有相关字段都是NOT NULL
.
For simplicity, assume all relevant fields are NOT NULL
.
你可以这样做:
SELECT
table1.this, table2.that, table2.somethingelse
FROM
table1, table2
WHERE
table1.foreignkey = table2.primarykey
AND (some other conditions)
否则:
SELECT
table1.this, table2.that, table2.somethingelse
FROM
table1 INNER JOIN table2
ON table1.foreignkey = table2.primarykey
WHERE
(some other conditions)
这两个在MySQL
中是否以相同的方式工作?
Do these two work on the same way in MySQL
?
推荐答案
INNER JOIN
是您应该使用的 ANSI 语法.
INNER JOIN
is ANSI syntax that you should use.
它通常被认为更具可读性,尤其是当您加入大量表格时.
It is generally considered more readable, especially when you join lots of tables.
也可以在需要时轻松替换为 OUTER JOIN
.
It can also be easily replaced with an OUTER JOIN
whenever a need arises.
WHERE
语法更面向关系模型.
The WHERE
syntax is more relational model oriented.
两个表的结果 JOIN
ed 是应用过滤器的表的笛卡尔积,过滤器只选择连接列匹配的那些行.
A result of two tables JOIN
ed is a cartesian product of the tables to which a filter is applied which selects only those rows with joining columns matching.
使用 WHERE
语法更容易看到这一点.
It's easier to see this with the WHERE
syntax.
就您的示例而言,在 MySQL(以及通常的 SQL)中,这两个查询是同义词.
As for your example, in MySQL (and in SQL generally) these two queries are synonyms.
另外,请注意 MySQL 还有一个 STRAIGHT_JOIN
子句.
Also, note that MySQL also has a STRAIGHT_JOIN
clause.
使用这个子句,可以控制JOIN
顺序:外循环扫描哪个表,内循环扫描哪个表.
Using this clause, you can control the JOIN
order: which table is scanned in the outer loop and which one is in the inner loop.
您无法在 MySQL 中使用 WHERE
语法来控制这一点.
You cannot control this in MySQL using WHERE
syntax.
这篇关于INNER JOIN ON vs WHERE 子句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:INNER JOIN ON vs WHERE 子句


- 如何使用 pip 安装 Python MySQLdb 模块? 2021-01-01
- 以一个值为轴心,但将一行上的数据按另一行分组? 2022-01-01
- 远程 mySQL 连接抛出“无法使用旧的不安全身份验证连接到 MySQL 4.1+"来自 XAMPP 的错误 2022-01-01
- 在SQL中,如何为每个组选择前2行 2021-01-01
- 导入具有可变标题的 Excel 文件 2021-01-01
- 如何将 SonarQube 6.7 从 MySQL 迁移到 postgresql 2022-01-01
- SQL 临时表问题 2022-01-01
- 如何将 Byte[] 插入 SQL Server VARBINARY 列 2021-01-01
- 更改自动增量起始编号? 2021-01-01
- 使用 Oracle PL/SQL developer 生成测试数据 2021-01-01