Oracle中沒有像MySQL那樣的GROUP_CONCAT
函數(shù),但是可以使用LISTAGG
函數(shù)或者WM_CONCAT
函數(shù)(在早期版本的Oracle中)來實現(xiàn)類似的功能
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優(yōu)化,這里有一些建議: