有个班级Class,还有个考试表Exam,如果能在考试表中查到班级的班号classId,则显示已安排,没有则显示未安排,这个sql语句怎么写啊
考试表可以认为就个classId我用的是 sql server ,主要是语句怎么写,一个语句是不不行啊,
查询结果类似下面的
ClassId    考场00001     已安排 
00002     未安排
麻烦给个例子,多谢!

解决方案 »

  1.   


    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
      

  2.   

    select classId,case when Exam.classId is null then N'已安排' else N'未安排' end  from Class left join Exam on Class.classId=Exam.classId where ....
      

  3.   


    select 
    classId, (case when Exam.classId is null then N'已安排' else N'未安排' end) AS '考场'
    from 
    Class 
    left join 
    Exam on Class.classId=Exam.classId
      

  4.   

    select ClassId , 考场 = '已安排' from Class where classId in (select classId from Exam) 
    union all 
    select ClassId , 考场 = '未安排' from Class where classId not in (select classId from Exam)
    order by classId