postgreSQL如何将表中的多行转换为JSON数组
postgres中的postgreSQL如何将表中的多行转换为JSON数组,sql,json,postgresql,Sql,Json,Postgresql,postgres中的ROW_TO_JSON函数将表中的一行转换为JSON对象 select row_to_json(result) from (select * from employee) as result; select row_to_json(result) from (select * from student) as result; 给我三行: {"salary":null,"age":65,"address":null,"id":111,"name":"NAME"} {"sala
ROW_TO_JSON
函数将表中的一行转换为JSON对象
select row_to_json(result) from (select * from employee) as result;
select row_to_json(result) from (select * from student) as result;
给我三行:
{"salary":null,"age":65,"address":null,"id":111,"name":"NAME"}
{"salary":null,"age":21,"address":null,"id":222,"name":"SURNAME"}
{"dob":"1997-03-02","name":"Mediocore","passed":true,"id":555}
前两行来自employee
表,而最后一行来自student
表
如果我想将单个表中的整个结果集放入JSON对象数组中,该怎么办?
例如
[{“工资”:空,“年龄”:65,“地址”:空,“id”:111,“姓名”:“姓名”},{“工资”:空,“年龄”:21,“地址”:空,“id”:222,“姓名”:“姓氏”}]
作为一行,而不是两行
是否有与
TABLE\u to_JSON
等效的东西?也许我遗漏了一些东西,但这看起来像是JSON\u agg
应该做的
您也不需要派生表:
select json_agg(row_to_json(employee))
from employee;
您运行的是哪个版本?.PostgreSQL 9.6和pgAdmin 4 v4ah,然后@a_horse_和_no_名称给出了最好的答案:)row_to_json是第一个json函数,您怎么知道OPs不是9.2上的?