MYSQL的group_concat()函数中实现将多行同一字段数据合并成一个数据

数据表 出访团组表
select a.t_applypersondocno,a.t_id from sx_fms_taskinfo a

结果集


数据表  团组和国家关联表
select * from sx_fms_taskinfoid_countryid

结果集


数据表  国家信息表
select c_id,c_name from sx_fms_countryinfo

结果集




进行关联后将出访国家组合到一起(组合前)
select taskinfo.t_applypersondocno, countryinfo.c_name from sx_fms_taskinfo taskinfo
left join sx_fms_taskinfoid_countryid tcinfo 
on taskinfo.t_id = tcinfo.t_id 
left join sx_fms_countryinfo countryinfo
on tcinfo.c_id = countryinfo.c_id 

组合前




进行关联后将出访国家组合到一起(组合后) 使用了 group_concat(c_name)
select taskinfo.t_applypersondocno,group_concat(c_name) from sx_fms_taskinfo taskinfo
left join sx_fms_taskinfoid_countryid tcinfo 
on taskinfo.t_id = tcinfo.t_id 
left join sx_fms_countryinfo countryinfo
on tcinfo.c_id = countryinfo.c_id 
group by taskinfo.t_applypersondocno 

组合后



猜你喜欢

转载自peter2009.iteye.com/blog/1672250