mysql - 从多个表中选择与 JOIN 比较

标签 mysql sql

我需要显示当前用户的所有赛程(谁与谁比赛),所以我编写了 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/

相关文章:

mysql - 匹配sql中的多个数字并获取行中所有匹配项的计数

PHP 搜索查询

php - 使用多个复选框返回 MySQL 结果

java - 如何在 Java 中使用 select 语句和连接表填充 jComboBox

php - 如何在 Yii2 中使用 leftJoin 从两个表中获取所有列数据

mysql - yii2 : Getting unknown property when using findBySql

sql - 在原始 sql 查询 Laravel 中插入变量

python - 将 pandas 时间序列数据导入本地主机上的 mysql 数据库时出错

javascript - 使用 jquery/php 在 mysql 中进行内联更新

Android工作室getSlotFromBufferLocked : unknown buffer error