0

I created these two tables:

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;

My query needs to return all information from game table for the top ten games based on GameScoreTotal.OverallScoreTotal.

Queries I have tried:

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;

Both tables have legitimate records within them.

4

1 回答 1

1

查询 1

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

查询 2

MySQL 没有FULL JOIN. 在此处阅读有关 MySQL JOIN 语法的信息。

试试GameGameScoreTotalLEFT 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;
于 2012-05-04T22:48:06.143 回答