我希望使用基于SQL Server数据库中的2个表的条件检索非重复数据。
表主题
ID NAME COURSE ------- ----------------------------------------- --------------------------------- SUB1234 SOFTWARE QUALITY AND PROCESS MANAGEMENT AUPAS AMERICAN UNIVERSITY PROGRAM SUB2234 SOFTWARE QUALITY AND PROCESS MANAGEMENT 1 AUPAS AMERICAN UNIVERSITY PROGRAM SUB3234 SOFTWARE QUALITY AND PROCESS MANAGEMENT 2 AUPAS AMERICAN UNIVERSITY PROGRAM
表格结果
ID SUBJECT RESULT
------- ------- ------
STU0001 SUB1234 NULL
期望的输出:
使用下面的查询,我只能检索非重复数据。
SELECT SUB.ID, SUB.NAME
FROM SUBJECT SUB LEFT JOIN
RESULT RES
ON RES.SUBJECT = SUB.ID
WHERE RES.SUBJECT IS NULL;
答案 0 :(得分:2)
此查询获得所需的结果。
SELECT SUB.ID, SUB.NAME
FROM SUBJECT SUB
WHERE NOT EXISTS (SELECT 1
FROM RESULT RES
WHERE RES.ID = 'STU0001'
AND RES.SUBJECT = SUB.ID)
AND SUB.COURSE = 'AUPAS AMERICAN UNIVERSITY PROGRAM';
基本上,查询获得课程='AUPAS AMERICAN UNIVERSITY PROGRAM'的所有科目,并排除学生已经关联的科目。
希望这有帮助!
答案 1 :(得分:0)
认为这就是你所追求的目标
select res.id as student, sub.id as 'subject'
from @result res, @subject sub
where sub.id != res.subj