0

嗨,他的查询给了我以下错误:

“on 子句”中的未知列“m.home_team_id”

SELECT DISTINCT (
`m`.`match_id`), 
`m`.`competition_id`, 
`m`.`date`, 
`h`.`team_name` AS "homeTeam", `h`.`team_id` AS "homeTeamID", 
`a`.`team_name` AS "awayTeam", `a`.`team_id` AS "awayTeamID", 
`o`.`for`, `o`.`against` 
FROM `single` bs, `matches` m, `teams` t, `outcomes` o 
JOIN `teams` `h` ON `m`.`home_team_id`=`h`.`team_id` 
JOIN `teams` `a` ON `m`.`away_team_id`=`a`.`team_id` 
WHERE `bs`.`match_id`=`m`.`match_id` 
AND `m`.`score_id`=`o`.`outcome_id` 
AND `actual_return` IS NOT NULL`enter code here`

m是一个名为 match 的表的别名,它有列home_team_idaway_team_id所以我不知道为什么找不到该列?

艾伦。

4

3 回答 3

1
SELECT 
     DISTINCT (`m`.`match_id`), 
      `m`.`competition_id`, 
      `m`.`date`, 

      `h`.`team_name` AS "homeTeam",
      `h`.`team_id` AS "homeTeamID", 

      `a`.`team_name` AS "awayTeam",
      `a`.`team_id` AS "awayTeamID", 

      `o`.`for`, 
      `o`.`against` 

FROM `single` bs

      join `matches` m ON bs.match_id = m.match_id
      join `outcomes` o m.score_id = o.outcome_id 
      join `teams` t   --<--- I don't know what the join is for `teams` here 
                          --- you probably don't need this in the query at all 

      JOIN `teams` `h` ON `m`.`home_team_id`=`h`.`team_id` 
      JOIN `teams` `a` ON `m`.`away_team_id`=`a`.`team_id` 

WHERE  `actual_return` IS NOT NULL 
于 2013-09-04T15:23:20.890 回答
1
FROM a, b, c
JOIN d ON ...
JOIN e ON ...

逻辑上的工作方式如下:

FROM a, b,
  (c
   JOIN d ON ...
   JOIN e ON ...)

因为JOIN关键字的优先级高于逗号。

因此,对于您的示例,当它尝试这样做时JOIN,它还不知道是什么m

所以你应该能够通过把它放在matches最后来修复它,即改变

FROM `single` bs, `matches` m, `teams` t, `outcomes` o 

至:

FROM `single` bs, `teams` t, `outcomes` o, `matches` m

但是,正如迈克尔建议的那样,您不应该混合使用显式连接(使用JOIN)和隐式连接(使用逗号)。

此外,teams t似乎根本没有使用。

于 2013-09-04T15:46:52.503 回答
0

我想你的错误是在哪里放置 JOINS。如果你想 JOIN 表m,你应该把 JOIN 放在它旁边,而不是放在 table 旁边o

我认为现在的方式是,该语句正在尝试将 tableo与 table连接起来h。这就是为什么它没有找到表(和列)m

于 2013-09-04T15:23:20.237 回答