有一张课程表,和一张课程计划表,想得到一张展示课程在对应时间是否有上课的信息的交叉表;
课程表
课程计划表
结果
create table course
(
c_id int,
c_name varchar(20)
);
create table course_plan
(
month int,
c_id int
);
insert into course(c_id, c_name)
values (1, 'java');
insert into course(c_id, c_name)
values (2, 'python');
insert into course(c_id, c_name)
values (3, 'sql');
insert into course_plan(month, c_id)
values (202209, 1);
insert into course_plan(month, c_id)
values (202209, 2);
insert into course_plan(month, c_id)
values (202210, 2);
insert into course_plan(month, c_id)
values (202210, 3);
insert into course_plan(month, c_id)
values (202211, 3);
insert into course_plan(month, c_id)
values (202211, 1);
select * from course;
select * from course_plan;
-- 交叉表
select c_name,
case
when (select count(c_id) from course_plan where month = '202209' and course_plan.c_id = course.c_id) > 0 then
'√'
else
'×'
end as '202209',
case
when (select count(c_id) from course_plan where month = '202210' and course_plan.c_id = course.c_id) > 0 then
'√'
else
'×'
end as '202210',
case
when (select count(c_id) from course_plan where month = '202211' and course_plan.c_id = course.c_id) > 0 then
'√'
else
'×'
end as '202211'
from course;