如何在SQL中使用join

时间:2017-04-07 16:11:12

标签: sql sql-server

我是SQL查询的初学者,我无法理解联接是如何工作的

这是我的SQL查询

SELECT 
    CAST (t_Users.UserID AS INT) AS UserID,
    t_Users.FirstName, 
    t_Users.LastName, 
    t_Users.Email,
    t_Users_In_Project.ProjectID,
    t_Teachers.TeacherID,
    t_Teachers.FirstName + ' ' + t_Teachers.LastName AS TeacherName,
    t_Teachers.Email AS TeacherEmail
FROM t_Users
LEFT JOIN t_Users_In_Project
ON t_Users_In_Project.UserID = t_Users.UserID
INNER JOIN t_Projects
ON t_Projects.ProjectID = t_Users_In_Project.ProjectID
LEFT JOIN t_Teachers
ON t_Teachers.TeacherID = t_Projects.TeacherID
WHERE t_Users_In_Project.ProjectID IS NOT NULL
ORDER BY t_Users.UserID ASC

我得到了这个结果

+--------+-----------+----------+-------------+-----------+-----------+-------------+--------------+
| UserID | FirstName | LastName |    Email    | ProjectID | TeacherID | TeacherName | TeacherEmail |
+--------+-----------+----------+-------------+-----------+-----------+-------------+--------------+
|    284 | Person    | A        | personA.com |      1951 |        94 | Teacher A   | teacherA.com |
|    284 | Person    | A        | personA.com |       245 |        68 | Teacher B   | teacherB.com |
|    284 | Person    | A        | personA.com |       139 |        41 | Teacher C   | teacherC.com |
|    284 | Person    | A        | personA.com |       176 |        41 | Teacher C   | teacherC.com |
+--------+-----------+----------+-------------+-----------+-----------+-------------+--------------+

同时我想要的是像

+--------+-----------+----------+-------------+-----------+-----------+-------------+--------------+
| UserID | FirstName | LastName |    Email    | ProjectID | TeacherID | TeacherName | TeacherEmail |
+--------+-----------+----------+-------------+-----------+-----------+-------------+--------------+
|    284 | Person    | A        | personA.com |      1951 |        94 | Teacher A   | teacherA.com |
|    284 | Person    | A        | personA.com |       245 |        68 | Teacher B   | teacherB.com |
|    284 | Person    | A        | personA.com |       176 |        41 | Teacher C   | teacherC.com |
+--------+-----------+----------+-------------+-----------+-----------+-------------+--------------+

基本上,我想让用户使用不同的教师而不是独特的项目。 如何使用SQL检索它?

数据库结构类似于

User
     UserID, FirstName, LastName Email
Users_in_project
     UserID, ProjectID
Project 
     ProjectID, TeacherID
Teacher
     TeacherID, FirstName, LastName, Email

谢谢

1 个答案:

答案 0 :(得分:1)

尝试使用max()group by这样的

select cast(t_Users.UserID as int) as UserID, t_Users.FirstName,
       t_Users.LastName, t_Users.Email, max(t_Users_In_Project.ProjectID) projectid,
       t_Teachers.AffiliateID,
       t_Teachers.FirstName + ' ' + t_Teachers.LastName as TeacherName,
       t_Teachers.Email as TeacherEmail
  from t_Users
  left join t_Users_In_Project on t_Users_In_Project.UserID = t_Users.UserID
 inner join t_Projects on t_Projects.ProjectID = t_Users_In_Project.ProjectID
  left join t_Teachers on t_Teachers.TeacherID = t_Projects.TeacherID
 where t_Users_In_Project.ProjectID is not null
 group by t_Users.UserID, t_Users.FirstName, t_Users.LastName, t_Users.Email,
          t_Teachers.AffiliateID, t_Teachers.FirstName, t_Teachers.LastName,
          t_Teachers.Email
 order by t_Users.UserID asc