表结构:
课程表t_lrm_course
讲师表t_lrm_lecturer
课程与讲师的关联表t_lrm_course_lecturer
目录表t_lrm_catalog
课程与目录的关联表t_lrm_course_catalog
用户表t_osm_user_info
课程授权表t_lrm_authority_user
需求:
需要查询出每个课程对应的讲师、目录和用户的信息。
要求按课程分条展示。
SQL语句:
select GROUP_CONCAT(DISTINCT(c.catalog_name) SEPARATOR ‘,‘) ‘catalog_name‘,a.course_code, a.course_name,a.school_time, case when a.course_type=‘online‘ then a.period when a.course_type=‘face2face‘ then CONCAT(a.period,case a.period_unit when ‘day‘ then ‘天‘ when ‘hour‘ then ‘小时‘ when ‘minute‘ then ‘分钟 end) end ‘period‘ , a.school_location,a.credit, GROUP_CONCAT(DISTINCT(case d.is_speaker when 1 then e.lecturer_name else ‘‘ end) SEPARATOR ‘‘) ‘speaker‘ , GROUP_CONCAT(DISTINCT(case d.is_speaker when 0 then e.lecturer_name else ‘‘ end) SEPARATOR ‘ ‘) ‘other_lecturer‘ , a.cost,a.course_source, case when INSTR(a.device_type,‘PC‘) >0 then ‘可‘ else ‘不可‘ end ‘pc_device_type‘, case when INSTR(a.device_type,‘MOBILE‘) >0 then ‘可‘ else ‘不可‘ end ‘mobile_device_type‘, case a.credit_requirement when ‘finishCourse‘ then ‘完成课程‘ when ‘gradePassed‘ then ‘考试成绩通过‘ end ‘credit_requirement‘, GROUP_CONCAT(DISTINCT(g.user_name) SEPARATOR ‘,‘) ‘authority_user‘ , case a.open_type when ‘partOpen‘ then ‘部分开放‘ when ‘allOpen‘ then ‘全部开放‘ when ‘close‘ then ‘不开放‘ end ‘open_type‘ from t_lrm_course a left join t_lrm_course_catalog b on a.course_id = b.course_id left join t_lrm_catalog c on c.catalog_id = b.catalog_id left join t_lrm_course_lecturer d on d.course_id = a.course_id left join t_lrm_lecturer e on e.lecturer_id = d.lecturer_id left join t_lrm_course_authority f on f.course_id = a.course_id left join t_osm_user_info g on g.user_id = f.user_id where 1=1 and a.course_id = ‘82977f40749b431fbef22e6356dea087‘ and a.course_type=‘face2face‘ group by a.course_id;
备注:
其中使用了mysql的多个函数:
- group_concat
- case when then end
- instr
时间: 2024-10-28 19:14:21