SQL 中 CASE 表达式的使用方式

  --- 使用IN谓词

  SELECT

  course_name AS '课程名',

  CASE WHEN courese_id IN (SELECT course_id FROM open_course WHERE mouth = '200706')

  THEN 'o'

  ELSE 'x'

  END AS '6 月'

  CASE WHEN courese_id IN (SELECT course_id FROM open_course WHERE mouth = '200707')

  THEN 'o'

  ELSE 'x'

  END AS '7 月'

  CASE WHEN courese_id IN (SELECT course_id FROM open_course WHERE mouth = '200708')

  THEN 'o'

  ELSE 'x'

  END AS '8 月'

  FROM course_master;

  --- 或者使用EXIST谓词

  SELECT CM.course_name,

  CASE WHEN EXISTS (SELECT course_id FROM OpenCourses OC WHERE month = 200706 AND OC.course_id = CM.course_id)

  THEN '○' ELSE '×'

  END AS "6 月",

  CASE WHEN EXISTS (SELECT course_id FROM OpenCourses OC WHERE month = 200707 AND OC.course_id = CM.course_id)

  THEN '○' ELSE '×'

  END AS "7 月",

  CASE WHEN EXISTS (SELECT course_id FROM OpenCourses OC WHERE month = 200708 AND OC.course_id = CM.course_id)

  THEN '○' ELSE '×'

  END AS "8 月"

  FROM CourseMaster CM;