Mysql 在其他表中选择条件时获取数据
我有两个表,现在我想要一个我在活动季节中选择的季节的所有比赛的输出。当我尝试下面的查询时,我得到一个错误。有人能帮我吗Mysql 在其他表中选择条件时获取数据,mysql,Mysql,我有两个表,现在我想要一个我在活动季节中选择的季节的所有比赛的输出。当我尝试下面的查询时,我得到一个错误。有人能帮我吗 SELECT * FROM `matches` Where season = active_season.season 错误:1054-where子句中的未知列“活动季节.季节” table matches id date season team_a team_b 1 2015-08-23 2015-2016 yellow red
SELECT * FROM `matches`
Where season = active_season.season
错误:1054-where子句中的未知列“活动季节.季节”
table matches
id date season team_a team_b
1 2015-08-23 2015-2016 yellow red
2 2015-04-18 2014-2015 green blue
3 2015-09-04 2015-2016 white brown
4 2014-02-11 2013-2014 pink yellow
5 2015-03-19 2014-2015 red brown
6 2015-11-30 2015-2016 blue pink
7 2015-05-06 2014-2015 green white
table active_season
id season
1 2015-2016
是的,它应该像你现在做的那样出错。您需要做的是执行一个连接操作,如
SELECT m.* FROM `matches` m
JOIN active_season ac ON m.season = ac.season;
或者在FROM子句中添加表active\u seasure,如
SELECT * FROM `matches`, active_season
Where season = active_season.season
在查询的SELECT或WHERE部分中使用表的字段时,它必须位于FROM部分。将SELECT视为资源交付部分,其中SELECT作为过滤部分,FROM作为资源保留区域,为前面提到的部分提供所需的资源 现在,当您在FROM部分中使用多个表时,MySQL将返回这些表的乘积。e、 g.如果您有以下两个表,其中包含给定的行:
table1 (id, title)
------------------
id title
------------------
1 first
2 second
及
然后运行这个查询
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
表1的每个记录与表2的每个记录相对应,即两个表的乘积。要获得正确的结果,需要指定table1的哪个记录与table2的哪个记录匹配。这可以使用零件或联接中的条件来完成
使用联接时将获得相同的结果
同样,您的问题也可以通过使用内部联接来解决
您可以在这里详细阅读MySQL连接:您介绍的是不属于这组表的表活动的概念:from子句或连接。db引擎是这样的,嗯,这是从哪里来的?但是无论你做什么,如果你加入,做一个显式的加入。就像2015年一样,虽然这段代码可以回答这个问题,但最好也提供一些解释来解释你的推理和它的作用。
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
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
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
SELECT
a.*
FROM
`matches` a
JOIN active_season b ON a.season = b.season
WHERE
b.season='2015-2016'
SELECT mac.*
FROM `matches` mac
JOIN
active_season ac
ON mac.season = ac.season;