学生
+----+-----------+--------------+
| id | First Name| Last Name |
+----+-----------+--------------+
| 1 | John | A |
+----+-----------+--------------+
| 2 | Jane | B |
+----+-----------+--------------+
认证
+----+------------------+
| id | Name |
+----+------------------+
| 1 | Certification 1 |
+----+------------------+
| 2 | Certification 2 |
+----+------------------+
| 3 | Certification 3 |
+----+------------------+
StudentCertifications
+----+------------+-----------------+
| id | StudentID | CertificationID |
+----+------------+-----------------+
| 1 | 1 | 1 |
+----+------------+-----------------+
| 2 | 1 | 2 |
+----+------------+-----------------+
| 3 | 1 | 3 |
+----+------------+-----------------+
我想通过SQL查询找到的内容:
StudentMissingCertifications
+----+------------+-----------------+
| id | StudentID | CertificationID |
+----+------------+-----------------+
| 1 | 2 | 1 |
+----+------------+-----------------+
| 2 | 2 | 2 |
+----+------------+-----------------+
| 3 | 2 | 3 |
+----+------------+-----------------+
答案 0 :(得分:3)
使用NOT EXISTS
SELECT * FROM Studients S CROSS JOIN Certifications C
WHERE NOT EXISTS
(
SELECT 1 FROM StudentCertifications SC WHERE SC.StudentId=S.Id AND SC.CertificationID=C.Id
)
或使用EXCEPT
SELECT S.ID,C.ID FROM Studients S CROSS JOIN Certifications C
EXCEPT
SELECT StudentId, CertificationID FROM StudentCertifications
答案 1 :(得分:1)
通过CROSS JOIN
将所有学生与所有认证相关联,然后使用NOT EXISTS
过滤已有的认证。
SELECT
StudentID = S.ID,
MissingCertificationID = C.ID
FROM
Student AS S
CROSS JOIN Certifications AS C
WHERE
NOT EXISTS (
SELECT
'student does not have the certification'
FROM
StudentCertifications X
WHERE
X.StudentID = S.ID AND
X.CertificationID = C.ID)
答案 2 :(得分:1)
交叉加入以获得所有可能的学生/证书,然后将其加入学生证书表,其中结果为空。
$ docker run --name artifactory -d -p 8081:8081 docker.bintray.io/jfrog/artifactory-oss:latest
答案 3 :(得分:1)
解决您的问题:
select Student.id,[First Name],[Last Name],Certifications.id,name
from Student cross join Certifications
left join StudentCertifications
on
Student.id=StudentCertifications.Studentid
WHERE StudentCertifications.Studentid IS NULL
<强>输出:强>
id First_Name Last_Name id name
2 Jane B 1 Certification 1
2 Jane B 2 Certification 2
2 Jane B 3 Certification 3
请点击演示链接:
答案 4 :(得分:0)
执行join
s(即LEFT JOIN
)查找尚未获得任何证书的学生
select
s.id as StudentID, c.id as CertificationID
from Student s
left join StudentCertifications sc on sc.StudentID = s.id
cross join (select id from Certifications) c
where sc.StudentID is null
答案 5 :(得分:0)
如果我理解了这个问题,那么这个查询就是你要找的
SELECT First Name, Last Name FROM Student
WHERE Student.id NOT IN (SELECT StudentID From StudentCertifications)