假设您有一个如下所示的表格布局:
couses:
id (INT), courseName (VARCHAR)
导师:
id (INT), courseId(INT), instructor(VARCHAR)
创建一个将打印所有课程的查询,如果有一个教师,则显示他们的名字,如果有两个教师,则按排序顺序在行上打印他们的名字,如果有两个以上的教师而不是教师姓名显示"委员会".
例如,您的输出看起来像这样
courseId instructor1 instructor2 0 Edward Yourdon 1 Edward Dijkstra Nicholas Wirth 2 Comittee
注:摘自TheDailyWtf的调查问卷.不是作业问题.
是的,是的,商业逻辑等.这是一个游戏,而不是你老板要求你这样做.
在T-SQL中:
select id , courseName , case (select count(*) from instructors i where i.courseid=c.courseid) when 0 then 'No Instructor' when 1 then (select top 1 instructor from instructors where i.courseid=c.courseid) when 2 then (select top 1 instructor from instructors where i.courseid=c.courseid order by instructor desc) else 'Committee' end as instructor_1 , case (select count(*) from instructors i where i.courseid=c.courseid) when 2 then (select top 1 instructor from instructors where i.courseid=c.courseid order by instructor asc) else '' end as instructor_2 from courses c