1、正常情况下的sql以及表

select t.* from pub_organ_type_view t;

合并两行数据并以逗号展示

上图明显有两处可以合并的记录

2、解决方案

①oracle数据库写法

在oracle11g以及其以后的版本中可以使用listagg函数,如下即可解决:

select organ_type,max(type_name) as type_name,
listagg(child_name, ',') within group(order by organ_type)as child_name
from pub_organ_type_view
group by organ_type;

合并两行数据并以逗号展示

②mysql数据库写法:使用group_conncat方法

SELECT organ_type, max(type_name) AS type_name, max(parent_type) AS parent_type, max(parent_name) AS parent_name,
group_concat(target_ref separator ',') AS child_id,
group_concat(child_name separator ',') AS child_name
FROM pub_organ_type_view
where 1 = 1
AND length(organ_type) = 1
GROUP BY organ_type

 

相关文章:

  • 2022-02-01
  • 2022-01-06
  • 2022-01-30
  • 2022-02-18
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2021-12-11
  • 2018-07-02
相关资源
相似解决方案