mysql分组多表查询_mysql多表查询分组统计

表结构:

课程表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

原文:http://my.oschina.net/xiaoxiangdaizi/blog/491797

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值