mysql:
语句:
select u.name1,group_concat(r.rolename,' ') from user1 u left join role r on u.id = r.user_id group by u.name1;
oracle:
语句:
select listagg(c.username,',') winthin group(order by c.username) from (select a.username from a left join b on a.username = b.loginname ) c group by username;
在实际开发中遇到一个问题,当username中合并的字符太多是会报错“字符串连接的结果过长”,这是因为,当合并完之后默认的类型是varchar类型,varchar字符长度只有4000,当超过4000时就会报错。所以此方法不适合数据量很大的地方,局限性比较大。
为了解决上述所遇到的问题换另一种方法即可完美解决,查出来的数据是clob类型:代码如下
select xmlagg(xmlparse(content c.username ||',' wellformed) order by c.username).getclobval() as username from (select a.username from a left join b on a.username = b.loginname ) c group by username;