我有一个包含多个字段(id、title、json_field)的表。我想从此表中选择所有行,但我希望 json_field 中的每个参数在结果中都是单独的一行。我想我应该使用 json_array_elements
,但问题是我无法理解如何将它包含在我的查询中。
有解决办法吗?我希望它能处理这个查询:
select id, title, json_array_elements(json_field) from table_name
,但它没有。
我也试过将子查询结果作为参数传递,也没有成功。
我通读了文档 ( http://www.postgresql.org/docs/9.3/static/functions-json.html ),但找不到任何有用的信息。
非常感谢您的帮助!
最佳答案
我尝试将 json_array_elements()
函数应用于 VARCHAR 和 TEXT 字段,但它不起作用:
HINT: No function matches the given name and argument types. You might need to add explicit type casts.
但是,它似乎可以很好地处理 JSON 类型。参见 SQLFiddle: http://sqlfiddle.com/#!15/2977f/3
所以您需要做的就是将您的字段转换为 JSON 数据类型: http://sqlfiddle.com/#!15/0fa19/1
关于sql - 在 PostgreSQL 中查询 JSON 字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25605522/