2012-02-26 87 views
14

我需要返回字符串列表的函數。MySQL中的聚合函數 - 列表(如Oracle中的LISTAGG)

我在表中的數據是這樣的:

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

我需要這樣的功能(像這樣在Oracle工作):

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

返回是這樣的:

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

有沒有想法?

+4

這是MySQL中的''GROUP_CONCAT()'](http://dev.mysql.com/doc/refman/5.0/en/group-by-functions.html#function_group-concat)。 – 2012-02-26 19:29:38

+0

[MySQL結果爲逗號分隔列表]的可能重複(http://stackoverflow.com/questions/662207/mysql-results-as-comma-separated-list) – 2012-02-26 19:30:50

回答

27

您正在尋找GROUP_CONCAT()

試試這個:

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

當然,你也可以group by結果。