問題描述
為簡單起見,假設所有相關字段都是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 子句的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!