Oracle中没有像MySQL那样的GROUP_CONCAT
函数,但是可以使用LISTAGG
函数或者WM_CONCAT
函数(在早期版本的Oracle中)来实现类似的功能
SELECT department_id, LISTAGG(employee_name, ',') WITHIN GROUP (ORDER BY employee_name) AS employee_names
FROM employees
GROUP BY department_id;
SELECT department_id, WM_CONCAT(employee_name) AS employee_names
FROM employees
GROUP BY department_id;
关于SQL优化,这里有一些建议: