无法在主键上使用UNION

时间:2014-04-12 02:46:50

标签: sql sql-server-2012 inner-join union

所以这些是2个查询:

SELECT * FROM CourseGrade; 
SELECT * FROM LetterGrades;

enter image description here

当我执行此UNION以查看哪个LetterGradeId具有哪个LetterGrade:

SELECT * 
    FROM CourseGrade
    INNER JOIN LetterGrades
    ON CourseGrade.LetterGradeId=LetterGrades.LetterGradeId; 

我收到错误说

Invalid column name 'LetterGradeId'.

谁能告诉我哪里出错?

3 个答案:

答案 0 :(得分:2)

你拼错了名字cg.LetterGradedId

SELECT * 
FROM CourseGrade cg
INNER JOIN LetterGrades lg
ON cg.LetterGradedId=lg.LetterGradeId;

答案 1 :(得分:1)

尝试这样:

SELECT * 
FROM CourseGrade cg
INNER JOIN LetterGrades lg
ON cg.LetterGradedId=lg.LetterGradeId; 

答案 2 :(得分:0)

您的查询是正确的,但您错过了拼写第一个表格中的列名称。

这应该有效:

     
SELECT * 
    FROM CourseGrade
    INNER JOIN LetterGrades
    ON CourseGrade.LetterGradedId=LetterGrades.LetterGradeId; 

请注意,您将colum命名为“LetterGrade d Id”而不是LetterGradeId:)