为什么我在字段列表中得到错误的字段或未知列?

时间:2012-05-04 22:39:15

标签: mysql sql

我创建了这两个表:

CREATE TABLE `Game` (

 `GameName` varchar(30) NOT NULL,
 `GameGenre` varchar(20) NOT NULL,
 `GameDescription` varchar(500) NOT NULL,
 `GameMode` varchar(20) NOT NULL,
 PRIMARY KEY (`GameName`)
) ENGINE=InnoDB;

CREATE TABLE `GameScoreTotal` (

 `ScoreID` int(3) NOT NULL AUTO_INCREMENT,
 `GameName` varchar(30) NOT NULL,
 `OverallScoreTotal` mediumint(9) NOT NULL,
 `GraphicsTotal` mediumint(9) NOT NULL,
 `StoryTotal` mediumint(9) NOT NULL,
 `GameplayTotal` mediumint(9) NOT NULL,
 `TimeTotal` mediumint(9) NOT NULL,
 PRIMARY KEY (`ScoreID`),
  CONSTRAINT  FOREIGN KEY (`GameName`) REFERENCES `Game` (`GameName`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB;

我的查询需要根据GameScoreTotal.OverallScoreTotal返回前十场比赛的游戏桌中的所有信息。

我试过的查询:

SELECT GameName 
FROM Game 
FULL JOIN GameScoreTotal 
    ON Game.GameName=GameScoreTotal.GameName 
ORDER BY OverallScoreTotal DESC 
LIMIT 0, 10;

SELECT Game.GameName 
FROM Game 
FULL JOIN GameScoreTotal 
    ON Game.GameName=GameScoreTotal.GameName 
ORDER BY OverallScoreTotal DESC 
LIMIT 0, 10;

两个表都包含合法记录。

1 个答案:

答案 0 :(得分:1)

查询1

在您的第一个查询中,问题是GameName和GameScoreTotal表中都存在GameName字段。因此错误Column 'GameName' in field list is ambiguous

查询2

MySQL没有 FULL JOIN Read here关于MySQL JOIN语法。

游戏 GameScoreTotal 表之间尝试 LEFT OUTER JOIN ,此联接将从Game表中获取所有记录以及所有匹配来自 GameScoreTotal 表的记录,如果 GameScoreTotal 表中没有匹配的记录,则会显示NULL。

您还可以尝试 INNER JOIN ,它只会获取两个表之间的匹配记录。

脚本

SELECT Game.GameName 
FROM Game 
LEFT OUTER JOIN GameScoreTotal 
    ON Game.GameName=GameScoreTotal.GameName 
ORDER BY OverallScoreTotal DESC 
LIMIT 0, 10;