当您在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