animal
id name
1 dog
1 cat
2 lion
怎么得到相同id的name list呢?也就是想得到id 为1的list为dog, cat格式。
1. 首先Oracle有一个方法,wm_concat()
select a.id
, wm_concat(a.name) as namelist
from animal a
group by a.id
但很遗憾,这个方法现在已经不support了.
2. 第二个方法XMLAGG()
select a.id
, (rtrim(xmlagg(xmlelement("xml"
, a.name||‘, ‘)
order by a.id).extract (‘//text()‘)
,‘, ‘)) as namelist
from animal a
group by a.id;
3. LISTAGG()
这个方法有些版本不支持,比如我的电脑和学校的。。
下面是官方用法:
SELECT department_id "Dept.", LISTAGG(last_name, ‘; ‘) WITHIN GROUP (ORDER BY hire_date) "Employees" FROM employees GROUP BY department_id ORDER BY department_id;
时间: 2024-11-06 23:38:58