使用json_populate_recordset而不创建表?

Ell*_*ner 10 sql postgresql json

我的数据库中有一个表,其中包含一个包含json记录的列.

id | json_records
---+-------------
 0 | "[{'x1' : 1234, 'x2' : 5678},{'x1' : 2345, 'x2' : 6789}]'
 1 | "[{'x1' : 4321, 'x2' : 8765},{'x1' : 5432, 'x2' : 9876}]'
Run Code Online (Sandbox Code Playgroud)

我想得到这样的东西:

id |   x1 |   x2
---+------+-----
 0 | 1234 | 5678
 0 | 2345 | 6789
 1 | 4321 | 8765
 1 | 5432 | 9876
Run Code Online (Sandbox Code Playgroud)

但我无法让查询工作:

select json_populate_recordset(json_records) from my_table
Run Code Online (Sandbox Code Playgroud)

我用json_populate_recordset看到的几个例子将结果插入表中,但我只是想返回结果.有没有办法在不创建新表的情况下执行此操作?

Sim*_*stö 11

您必须创建一个新类型以传递给该函数(请注意,作为json_populate返回,json_type您必须使用(row).*符号来获取单个字段):

CREATE type json_type AS (x1 int, x2 int);

 SELECT id, (json_populate_recordset(null::json_type, json_records)).* FROM my_table;
Run Code Online (Sandbox Code Playgroud)