我有一个 MySQL 查询问题。我建立了一个记录高中橄榄球分数的表格。它看起来像这样:
CREATE TABLE `games` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`home_team` int(11) NOT NULL,
`visitor_team` int(11) NOT NULL,
`home_score` tinyint(4) NOT NULL,
`visitor_score` tinyint(4) NOT NULL,
`quarter` tinyint(4) NOT NULL,
`week` tinyint(2) NOT NULL,
`game_date` date NOT NULL,
`game_time` time NOT NULL,
`complete` tinyint(4) NOT NULL DEFAULT '0',
`stadium_id` int(11) NOT NULL,
`created_at` datetime NOT NULL,
`updated_at` datetime NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=151;
我正在执行的查询使用团队的 id 进行比较。我想检索给定团队 id 仅击败的团队的 id 值。到目前为止,这是我的查询。
SELECT
CASE
WHEN games.home_team != ? AND (games.home_score > games.visitor_score) THEN games.home_team
WHEN games.visitor_team != ? AND (games.home_score < games.visitor_score) THEN games.visitor_team
END AS id, teams.class_id, classes.name
FROM games
INNER JOIN teams ON id = teams.id
INNER JOIN classes ON teams.class_id = class.id
WHERE games.complete = 1
当我在 PHP MyAdmin 中运行此查询时,我收到以下错误:
#1052 - Column 'id' in on clause is ambiguous
我正在使用 PDO 的 execute(),所以 ? 代表团队 id 值。我想既然我将 CASE 语句的结果分配给了 id,那么我可以使用它来加入团队表。谁能指出我正确的方向来解决这个错误?先感谢您。