postgresql string_agg
时间: 2023-10-16 07:10:59 浏览: 92
The PostgreSQL function `string_agg` is used to concatenate multiple strings into a single string, separated by a specified delimiter. The syntax for `string_agg` is as follows:
```
string_agg(expression, delimiter)
```
Where `expression` is the string to be concatenated and `delimiter` is the character(s) to be used to separate the concatenated strings.
For example, suppose we have a table named `employees` with columns `id`, `name`, and `department`. We want to concatenate all the employee names in a particular department, separated by commas. We can use the following query:
```
SELECT department, string_agg(name, ',') AS employee_names
FROM employees
WHERE department = 'Sales'
GROUP BY department;
```
This will return a result set with the department name and a comma-separated list of employee names in that department. The `GROUP BY` clause is used to group the results by department.
阅读全文