postgresql 查询结果 多行变一行
2016-09-28 13:12
204 查看
1.SELECT a.id, a.db_name,
string_agg(b.username, ',') FROM tbl_company a, tbl_user b WHERE a.id = b.company_id group by a.id
查询字段只能是字符型 结果用“,”隔开
2.SELECT a.id, a.db_name, array_agg(b.username) FROM tbl_company a,
tbl_user b WHERE a.id = b.company_id group by a.id
查询字段不分类型 结果为数组{1,2,3}或 {123,123admin}
SELECT a.id, a.db_name, array_to_string(array_agg(b.username), ',') FROM tbl_company a, tbl_user b WHERE a.id = b.company_id group by a.id
结果是字符串 123,123admin
array_agg(distinct(字段名)) 拼接唯一的字段
string_agg(b.username, ',') FROM tbl_company a, tbl_user b WHERE a.id = b.company_id group by a.id
查询字段只能是字符型 结果用“,”隔开
2.SELECT a.id, a.db_name, array_agg(b.username) FROM tbl_company a,
tbl_user b WHERE a.id = b.company_id group by a.id
查询字段不分类型 结果为数组{1,2,3}或 {123,123admin}
SELECT a.id, a.db_name, array_to_string(array_agg(b.username), ',') FROM tbl_company a, tbl_user b WHERE a.id = b.company_id group by a.id
结果是字符串 123,123admin
array_agg(distinct(字段名)) 拼接唯一的字段
相关文章推荐
- SQLERROR:1267
- redis cluster
- [High.Performance.MySQL(3rd,2012.3)].Baron.Schwartz.之数据类型优化
- 数据库备份和还原策略优缺点浅谈
- 从2级C中了解数据库基础
- [Oracle] CPU/PSU补丁安装详细教程
- MySQL删除所有数据时报错
- Mysql表导入excel数据
- mysql group by
- mysql修改字段字符集
- MySQL Clent 设置编码
- Oracle 补丁及opatch 工具介绍
- mongodb分片集群的管理命令集合
- 解决新恢复的数据库在凭证序时薄上反过账的按钮处理方式
- MyBatis系列:(9)mybatis-spring-springmvc-oracle/mysql整合
- 常用SQL语句(1)
- MyCat:开源分布式数据库中间件
- for update造成的Oracle锁表与解锁
- Oracle 12cR1新特性导致的性能问题---MMON进程消耗了很高的CPU使用率.
- Redis集合类型的常用命令小结