GROUP_CONCAT()函数
GROUP_CONCAT函数返回一个字符串结果,该结果由分组中的值连接组合而成。常和groupby连用
需求:以学员维度列举学员对应的学科code,
表数据
查询有一个学员id对应多个subject_code数据,所以需要拼接数据展示
SELECT student_id, GROUP_CONCAT(subject_code) FROM crm_student_subject GROUP BY student_id
继续优化,关联字典定义表,展示学科中文名称
1.将字典中的学科对应名称查询展示
2.学员学科表作为主表,关联字典定义表,展示学员id对应学科名称
3.group_concat()函数
SELECT subjectname.student_id, GROUP_CONCAT(subjectname.name) 'subject' FROM (SELECT student_id, itemsubject.name FROM crm_student_subject crm_subject LEFT JOIN (SELECT CODE, NAME FROM `crm_define_item` WHERE belong = 'apply_aim_subject' GROUP BY CODE, NAME) itemsubject ON crm_subject.subject_code = itemsubject.code) subjectname GROUP BY subjectname.student_id