標籤:
表結構:
課程表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 aleft join t_lrm_course_catalog b on a.course_id = b.course_idleft join t_lrm_catalog c on c.catalog_id = b.catalog_idleft join t_lrm_course_lecturer d on d.course_id = a.course_idleft join t_lrm_lecturer e on e.lecturer_id = d.lecturer_idleft join t_lrm_course_authority f on f.course_id = a.course_idleft join t_osm_user_info g on g.user_id = f.user_idwhere 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
mysql多表查詢分組統計