无法找到MySQL列

时间:2013-09-04 15:15:19

标签: mysql

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

'on clause'

中的未知列'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是名为匹配的表的别名,其中包含home_team_id列和away_team_id列,因此我无法确定无法找到列的原因?

艾伦。

3 个答案:

答案 0 :(得分: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 

答案 1 :(得分: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

答案 2 :(得分:0)

我猜你的错误在于放置JOINS的位置。如果您想加入表格m,则应将JOIN放在旁边,而不是表o旁边。

我认为现在的方式是,该语句试图将表o与表h连接起来。这就是为什么它找不到表(和列)m