Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
postgreSQL如何将表中的多行转换为JSON数组_Sql_Json_Postgresql - Fatal编程技术网

postgreSQL如何将表中的多行转换为JSON数组

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

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"}
{"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上的?