有个班级Class,还有个考试表Exam,如果能在考试表中查到班级的班号classId,则显示已安排,没有则显示未安排,这个sql语句怎么写啊
考试表可以认为就个classId我用的是 sql server ,主要是语句怎么写,一个语句是不不行啊,
查询结果类似下面的
ClassId 考场00001 已安排
00002 未安排
麻烦给个例子,多谢!
考试表可以认为就个classId我用的是 sql server ,主要是语句怎么写,一个语句是不不行啊,
查询结果类似下面的
ClassId 考场00001 已安排
00002 未安排
麻烦给个例子,多谢!
select ClassId , 考场 = '已安排' from Class where exists(select 1 from Exam where classId = class.classId)
union all
select ClassId , 考场 = '未安排' from Class where not exists(select 1 from Exam where classId = class.classId)
order by classId
select
classId, (case when Exam.classId is null then N'已安排' else N'未安排' end) AS '考场'
from
Class
left join
Exam on Class.classId=Exam.classId
union all
select ClassId , 考场 = '未安排' from Class where classId not in (select classId from Exam)
order by classId