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優化,這里有一些建議: