语法不正确; EE.EnemyId无法受约束

时间:2018-04-18 01:18:48

标签: sql sql-server syntax

SELECT YEAR(EpisodeDate), EN.EnemyName, Count (*) AS [Number of Episodes]
FROM tblEpisode AS E INNER JOIN
     tblEpisodeEnemy AS EE
     ON EE.EpisodeId = E.EpisodeId INNER JOIN
     tblEnemy AS EN INNER JOIN
     ON EE.EnemyId = EN.EnemyId
GROUP BY EpisodeDate, EnemyName 

2 个答案:

答案 0 :(得分:1)

这不会修复您的语法错误(这是因为您引用了表中不存在的列)。

但是,毫无疑问,你想要每年一排。因此,您需要修复GROUP BY

SELECT YEAR(EpisodeDate), EN.EnemyName, Count(*) AS num_episodes
FROM tblEpisode E INNER JOIN
     tblEpisodeEnemy EE
     ON EE.EpisodeId = E.EpisodeId INNER JOIN
     tblEnemy EN INNER JOIN
     ON EE.EnemyId = EN.EnemyId
GROUP BY YEAR(EpisodeDate), EN.EnemyName 

答案 1 :(得分:0)

由于额外的INNER JOIN而导致语法错误

SELECT YEAR(EpisodeDate), EN.EnemyName, Count (*) AS [Number of Episodes]
FROM tblEpisode AS E INNER JOIN
     tblEpisodeEnemy AS EE
     ON EE.EpisodeId = E.EpisodeId INNER JOIN
     tblEnemy AS EN /*INNER JOIN*/
     ON EE.EnemyId = EN.EnemyId
GROUP BY EpisodeDate, EnemyName