當您在SELECT
或WHERE
零件的使用表的字段查詢,它必須在FROM
部分。將SELECT
視爲資源傳遞部分,將WHERE
視爲過濾部分,將FROM
視爲爲前述部分提供所需資源的資源佔用區域。
現在,當您在FROM
部分中使用多個表時,MySQL會返回這些表的一個產品。例如如果你有以下兩個表給出行:
table1 (id, title)
------------------
id title
------------------
1 first
2 second
和
table2 (id, fk_id, description) // fk_id is foreign key from table1
-------------------------------------
id fk_id description
1 1 d1
2 2 d2
並運行此查詢
SELECT * FROM table1, table2
你得到這樣的結果:
id title id fk_id description
-----------------------------------------
1 first 1 1 d1
1 first 2 2 d2
2 second 1 1 d1
2 second 2 2 d2
每記錄針對table2
的每個記錄即,即兩個表的乘積。要獲得正確的結果,您需要指定table1的哪個記錄與table2的哪個記錄相匹配。這可以使用條件來完成在WHERE
部分或JOIN
SELECT * FROM table1, table2 WHERE table1.id=table2.fk_id
-----------------------------------------
id title id fk_id description
-----------------------------------------
1 first 1 1 d1
2 second 2 2 d2
同樣的結果將使用JOIN
SELECT * FROM table1 JOIN table2 ON table1.id=table2.fk_id
-----------------------------------------
id title id fk_id description
-----------------------------------------
1 first 1 1 d1
2 second 2 2 d2
同樣的問題時,可以通過使用INNER JOIN
SELECT
a.*
FROM
`matches` a
JOIN active_season b ON a.season = b.season
WHERE
b.season='2015-2016'
來解決實現
你可以在這裏詳細閱讀關於MySQL連接:https://dev.mysql.com/doc/refman/5.0/en/join.html
您正在介紹不屬於這組表的一部分的表active_season:(from子句或聯接)。數據庫引擎就像是,這是從哪裏來的? – Drew
http://www.w3schools.com/sql/sql_join.asp – user2268997
但無論你做什麼,如果你加入,做一個**顯式加入**。這就像是2015年 – Drew