mysql多表查詢分組統計

來源:互聯網
上載者:User

標籤:

表結構:

課程表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的多個函數:

  1. group_concat

  2. case when then end

  3. instr

mysql多表查詢分組統計

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.