1

嗨,我有一个看起来像这样的表之间的 UNION:

SELECT GetStudentClassmates.AvatarFileName  as AvatarFileName,
       GetStudentClassmates.UserName        as UserName,
       GetStudentClassmates.RoleId          as RoleId
FROM GetStudentClassmates
WHERE GetStudentClassmates.StudentId = @studentId

UNION

SELECT GetStudentTeachers.AvatarFileName    as AvatarFileName,
       GetStudentTeachers.UserName          as UserName,
       GetStudentTeachers.RoleId            as RoleId
FROM GetStudentTeachers

现在我需要将 UNION 的结果与其他表连接起来,并在最终结果中添加另一列。

这是我正在使用的 JOIN 代码

LEFT OUTER JOIN Sync_StudentAssignments as StudentAssignments 
  ON @studentId = StudentAssignments.StudentID 
  AND StudentAssignments.Deletion_Date IS NULL
LEFT OUTER JOIN Sync_StudentAssignmentInstances as StudentAssignmentInstances 
  ON StudentAssignments.StudentAssignmentID = StudentAssignmentInstances.StudentAssignmentID 
  AND StudentAssignmentInstances.Deletion_Date IS NULL

我的问题是我不知道必须在哪里添加选择代码,我认为这看起来像这样:

SELECT StudentAssignmentInstances.ModuleInstanceId

我的决赛桌应该有以下列:

AvatarFileName,
UserName,
RoleId,
ModuleInstanceId

我怎样才能做到这一点?

4

3 回答 3

4

将您的UNION位括在括号中并将其用作内联表,然后正常加入:

-----------------------------------------------------------
-- choose the exact columns you need here using the aliases
-----------------------------------------------------------
select x.*, ssa.*, ssai.* from
(
   select ....
   union
   select ....
) as x
inner join Sync_StudentAssignments ssa on ...
inner join Sync_StudentAssignmentInstances ssai on ....
于 2013-07-16T09:05:30.483 回答
2

在 SQL Server 中,如果要加入某个联合,可以使用子选择表单或 CTE。在这里我会使用 CTE,因为它在 TSQL 代码中添加了一些顺序......

;WITH Participants AS (
  SELECT GetStudentClassmates.AvatarFileName  as AvatarFileName,
         GetStudentClassmates.UserName        as UserName,
         GetStudentClassmates.RoleId          as RoleId
  FROM GetStudentClassmates
  WHERE GetStudentClassmates.StudentId = @studentId

  UNION ALL

  SELECT GetStudentTeachers.AvatarFileName    as AvatarFileName,
         GetStudentTeachers.UserName          as UserName,
         GetStudentTeachers.RoleId            as RoleId
  FROM GetStudentTeachers)
SELECT
FROM 
  Participants p
  JOIN WhateverYouWant wyw
    ON wyw.{column1} = p.{column1}
    AND wyw.{column2} = p.{column2}
    ...

这里WhateverYouWant可能是你的Sync_StudentAssignments JOIN Sync_StudentAssignmentInstances. 但是,您已经使用变量和 const 加入了这一点,这将给出笛卡尔积,我相信这不是您的意图。

编辑

现在我更仔细地查看了您的条件,我相信您必须执行以下操作:

;WITH Participants AS (
  SELECT GetStudentClassmates.AvatarFileName,
         GetStudentClassmates.UserName,
         GetStudentClassmates.RoleId,
         GetStudentClassmates.StudentId       
  FROM GetStudentClassmates
  --WHERE GetStudentClassmates.StudentId = @studentId
  --uncomment previous line only if you need just one student in your recordset

  UNION ALL

  SELECT GetStudentTeachers.AvatarFileName,
         GetStudentTeachers.UserName,
         GetStudentTeachers.RoleId,
         GetStudentClassmates.TeacherId AS StudentId -- just guessing column name
  FROM GetStudentTeachers
  --previous select means you need all of the teachers, no matter which student
)
SELECT
  p.AvatarFileName,
  p.UserName,
  p.RoleId,
  StudentAssignmentInstances.ModuleInstanceId
FROM 
  Participants p
  LEFT JOIN Sync_StudentAssignments as StudentAssignments 
    ON p.StudentId = StudentAssignments.StudentID 
    AND StudentAssignments.Deletion_Date IS NULL
  LEFT JOIN Sync_StudentAssignmentInstances as StudentAssignmentInstances 
    ON StudentAssignments.StudentAssignmentID = StudentAssignmentInstances.StudentAssignmentID 
    AND StudentAssignmentInstances.Deletion_Date IS NULL
于 2013-07-16T09:10:15.003 回答
1
SELECT tbl.AvartarFileName, 
       tbl.UserName, 
       tbl.RoleId, 
       StudentAssignmentInstances.ModuleInstanceId
FROM
(
    SELECT GetStudentClassmates.AvatarFileName  as AvatarFileName,
           GetStudentClassmates.UserName        as UserName,
           GetStudentClassmates.RoleId          as RoleId
      FROM GetStudentClassmates
     WHERE GetStudentClassmates.StudentId = @studentId

     UNION

    SELECT GetStudentTeachers.AvatarFileName    as AvatarFileName,
           GetStudentTeachers.UserName          as UserName,
           GetStudentTeachers.RoleId            as RoleId
           FROM GetStudentTeachers
) AS tbl
LEFT OUTER JOIN Sync_StudentAssignments as StudentAssignments 
  ON @studentId = StudentAssignments.StudentID 
  AND StudentAssignments.Deletion_Date IS NULL
LEFT OUTER JOIN Sync_StudentAssignmentInstances as StudentAssignmentInstances 
  ON StudentAssignments.StudentAssignmentID = StudentAssignmentInstances.StudentAssignmentID 
  AND StudentAssignmentInstances.Deletion_Date IS NULL
于 2013-07-16T09:14:22.787 回答