我需要显示当前用户的所有赛程(谁与谁比赛),所以我编写了 SQL 查询
SELECT
fixture.*
FROM
sport_team_player AS team_player, sport_team AS team
INNER JOIN sport_fixture AS fixture
ON (`team_player`.`team_id` = fixture.`team1_id` OR `team_player`.`team_id` = fixture.`team2_id`)
WHERE
team_player.`team_id` = team.`team_id` AND team_player.`player_id` = '16'
这不起作用并告诉我 team_player.team_id 不存在
但是如果我加入第二个表而不是从多个表中进行选择,它就可以正常工作。
PS。这不是编写此类查询的最佳方式,但它是由 ORM 模块生成的。编辑:
结果将是夹具数据列表,例如
------------------------------
|fixture_id|team1_id|team2_id|
------------------------------
|1 | 2 | 3 |
------------------------------
最佳答案
试试这个。应该产生与您相同的查询;
SELECT fixture.*
FROM sport_team_player AS team_player
JOIN sport_team AS team
ON team_player.`team_id` = team.`team_id` AND team_player.`player_id` = '16'
INNER JOIN sport_fixture AS fixture
ON (`team_player`.`team_id` = fixture.`team1_id`
OR `team_player`.`team_id` = fixture.`team2_id`)
在构建连接时,不应混淆这两种表示法。您用来连接 team_player 和 team 的逗号,以及随后对内部连接的调用,很可能会触发未知列错误。
关于mysql - 从多个表中选择与 JOIN 比较,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19242540/