Mysql 组_CONCAT未合并字段
我在使用GROUP_CONTACT时遇到语法问题 使用如下所示的sql语句:Mysql 组_CONCAT未合并字段,mysql,Mysql,我在使用GROUP_CONTACT时遇到语法问题 使用如下所示的sql语句: SELECT s.school_code , s.school_name ,st.subject ,sg.subgroup ,GROUP_CONCAT(IF(r.year=2012,a.proficiency_index,NULL)) AS pi_2012 ,GROUP_CONCAT(IF(r.year=2013,a.proficiency_index,NULL))
SELECT
s.school_code
, s.school_name
,st.subject
,sg.subgroup
,GROUP_CONCAT(IF(r.year=2012,a.proficiency_index,NULL)) AS pi_2012
,GROUP_CONCAT(IF(r.year=2013,a.proficiency_index,NULL)) AS pi_2013
FROM
ayp_data a
INNER JOIN
report_year r ON
a.report_year_id = r.id
INNER JOIN
school s ON
a.school_code_id = s.id
INNER JOIN
sub_group sg ON
a.subgroup_id = sg.id
INNER JOIN
`subject` st ON
a.subject_id = st.id
GROUP BY
report_year_id,
s.school_code
, s.school_name
,st.subject
,sg.subgroup
HAVING
s.school_name = 'Moody Elementary School' AND
`subject` = 'Mathematics' AND
`subgroup` = 'All Students'
SCHOOL_CODE SCHOOL_NAME SUBJECT SUBGROUP PI_2012 PI_2013
0065 Moody Elementary School Mathematics All Students 9.640000343322754 (null)
0065 Moody Elementary School Mathematics All Students (null) 10.920000076293945
我得到的结果如下:
SELECT
s.school_code
, s.school_name
,st.subject
,sg.subgroup
,GROUP_CONCAT(IF(r.year=2012,a.proficiency_index,NULL)) AS pi_2012
,GROUP_CONCAT(IF(r.year=2013,a.proficiency_index,NULL)) AS pi_2013
FROM
ayp_data a
INNER JOIN
report_year r ON
a.report_year_id = r.id
INNER JOIN
school s ON
a.school_code_id = s.id
INNER JOIN
sub_group sg ON
a.subgroup_id = sg.id
INNER JOIN
`subject` st ON
a.subject_id = st.id
GROUP BY
report_year_id,
s.school_code
, s.school_name
,st.subject
,sg.subgroup
HAVING
s.school_name = 'Moody Elementary School' AND
`subject` = 'Mathematics' AND
`subgroup` = 'All Students'
SCHOOL_CODE SCHOOL_NAME SUBJECT SUBGROUP PI_2012 PI_2013
0065 Moody Elementary School Mathematics All Students 9.640000343322754 (null)
0065 Moody Elementary School Mathematics All Students (null) 10.920000076293945
我想将两行合并为一行,并将非空字段值PI_2012和PI_2013放在同一行上
我想我可以通过小组接触做到这一点;但它并不像我想象的那样
如何使用GROUP_CONCAT合并这些字段
或者,有没有更聪明的方法
我有完整的架构和查询。如果您不想在单独的行中显示单独的年份,那么您需要从
GROUP BY
子句中删除报告年份id
。也就是说,您需要更改以下内容:
GROUP BY
report_year_id,
s.school_code
, s.school_name
,st.subject
,sg.subgroup
为此:
GROUP BY
s.school_code
, s.school_name
,st.subject
,sg.subgroup