数据库值显示NULL

时间:2016-10-04 02:38:28

标签: sql sql-server-2008

我有两个数据库。 表1(学生)具有StudentID,EmployeeClass,StudentName字段。这里,EmployeeClass是varchar(5)。 表2(Employee)具有EmployeeID,EmployeeName,Description和EmployeeClass字段。 Employee Class是nvarchar(5)。

当我将Select Query编写为:

          SELECT S.StudentID, 
                 S.EmployeeClass, 
                 S.StudentName 
            FROM Student AS S
 LEFT OUTER JOIN Employee AS E
              ON CAST(S.EmployeeClass AS VARCHAR(5)) = E.EmployeeID
           WHERE E.Description = 'ABC'

所有值来自employee表显示为NULL?如何解决它

1 个答案:

答案 0 :(得分:0)

我认为你的script.column中的连接条件是错误的。在这个脚本中,'EmployeeClass'指的是'EmployeeID'。

SELECT S.StudentID, 
              S.EmployeeClass, 
              S.StudentName
 FROM Student AS S
  LEFT  JOIN Employee AS E
          ON cast(S.EmployeeClass  as varchar(5))= E.EmployeeClass 
             AND  E.Description = 'ABC'