string_agg:
示例场景
假设有一个订单表:
| customer | product |
|---|---|
| 张三 | 苹果 |
| 张三 | 香蕉 |
| 李四 | 橘子 |
查询每个客户购买的所有商品:
SELECT customer,string_agg(product, ', ') AS products
FROM orders
GROUP BY customer;
结果:
| customer | products |
|---|---|
| 张三 | 苹果, 香蕉 |
| 李四 | 橘子 |
string_agg:
假设有一个订单表:
| customer | product |
|---|---|
| 张三 | 苹果 |
| 张三 | 香蕉 |
| 李四 | 橘子 |
查询每个客户购买的所有商品:
SELECT customer,string_agg(product, ', ') AS products
FROM orders
GROUP BY customer;
结果:
| customer | products |
|---|---|
| 张三 | 苹果, 香蕉 |
| 李四 | 橘子 |