我的sql语句中有一个情况。在这个sql语句中,我一起加入了3个表(Application_Detail,Teacher_Detail和Class_Detail),,而不是我使用WHERE
来过滤我的表,看看有多少老师使用过这个应用程序,我发现了以下结果。
正如您在第1条记录中所看到的,教师相关字段和类相关字段均为空。我想知道有没有办法过滤掉第一条记录,只显示2,3,4记录?因为我想只显示记录,如果在teacherId,teacherName,class或grade列中有一些值。
teacherId teacherName applicationName class grade
1. NULL NULL Excel NULL NULL
2. 5 NULL Excel NULL NULL
3. NULL NULL Excel A 6
4 NULL NULL Excel B 2
这是我的SQL命令
SELECT
td.teacherId,
teacherName,
applicationName,
class,
grade
FROM
[AppUser_Detail] as aud
LEFT OUTER JOIN [Teacher_Detail] as td ON aud.teacherId = td.teacherId
LEFT OUTER JOIN [Application_Detail] as ad ON aud.applicationId = ad.applicationId
LEFT OUTER JOIN [Class_Detail] as cd ON aud.classId = cd.classId
WHERE
aud.applicationId = 6 //I filter if my application Id is 6
答案 0 :(得分:5)
试试这个:
SELECT
td.teacherId,
teacherName,
applicationName,
class,
grade
FROM
[AppUser_Detail] as aud
LEFT OUTER JOIN [Teacher_Detail] as td ON aud.teacherId = td.teacherId
LEFT OUTER JOIN [Application_Detail] as ad ON aud.applicationId = ad.applicationId
LEFT OUTER JOIN [Class_Detail] as cd ON aud.classId = cd.classId
WHERE
td.teacherId is not null OR class is not null OR grade is not null
答案 1 :(得分:2)
SELECT
td.teacherId,
teacherName,
applicationName,
class,
grade
FROM [AppUser_Detail] as aud
LEFT OUTER JOIN [Teacher_Detail] as td ON aud.teacherId = td.teacherId
LEFT OUTER JOIN [Application_Detail] as ad ON aud.applicationId = ad.applicationId
LEFT OUTER JOIN [Class_Detail] as cd ON aud.classId = cd.classId
WHERE
aud.applicationId = 6 //I filter if my application Id is 6
AND NOT (td.teacherId IS NULL AND class IS NULL AND grade IS NULL)
答案 2 :(得分:0)
SELECT *
FROM (
SELECT td.teacherId AS [TeacherID]
,teacherName AS [TeacherName]
,applicationName AS [ApplicationName]
,class AS [Class]
,grade AS [Grade]
FROM [AppUser_Detail] AS aud
LEFT JOIN [Teacher_Detail] AS td ON aud.teacherId = td.teacherId
LEFT JOIN [Application_Detail] AS ad ON aud.applicationId = ad.applicationId
LEFT JOIN [Class_Detail] AS cd ON aud.classId = cd.classId
WHERE aud.applicationId = 6
)
WHERE TeacherID IS NOT NULL
OR TeacherName IS NOT NULL
OR Grade IS NOT NULL