Aggregate function in MySQL - list (like LISTAGG in Oracle)

妖精的绣舞 提交于 2019-11-26 09:47:23

问题


I need function, that returns list of strings.

I have data in table like this:

Id    MyString
------------------------
 1    First
 2    Second
 3    Third
 4    Fourth

I need function like this (something like this works in oracle):

select LISTAGG(MyString, \', \') as myList where id < 4

That returns something like this:

myList
------------------------
First, Second, Third

Any ideas?


回答1:


You're looking for GROUP_CONCAT()

Try this:

select group_concat(MyString separator ', ') as myList from table
where id < 4

Of course, you can group by the results.




回答2:


As of MySQL 5.7.22 you can also use two JSON aggregation functions: JSON_ARRAYAGG or JSON_OBJECTAGG. You can combine these together with MySQL's JSON functions to get results aggregated as JSON. Unlike GROUP_CONCAT, there is no MySQL configuration parameter which would limit the size of the returned value, other than max_allowed_packet (which affects all queries).



来源:https://stackoverflow.com/questions/9456380/aggregate-function-in-mysql-list-like-listagg-in-oracle

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!