简单查询工作正常:
SELECT json_array_elements_text('["first", "third", "second"]'::json)
Run Code Online (Sandbox Code Playgroud)
但我也想以某种方式检索数组键,因此输出如下:
key value
0 first
1 third
2 second
Run Code Online (Sandbox Code Playgroud)
UPD
好像 row_number() 是 ap?r?o?p?e?r? 解决方案,但我不知道如何进一步使用它。
假设我有“帖子”表,每个帖子都包含一组 JSON 格式的相关评论:
SELECT id, title, comments FROM posts
id title comments
1 Title 1 ["comment 1", "comment 2"]
2 Title 2 ["comment 3", "comment 4", "comment 5"]
3 Title 3 ["comment 6"]
Run Code Online (Sandbox Code Playgroud)
目标不仅是扩展评论值,而且是扩展键:
Tricky SQL here
id title comment key
1 Title 1 comment 1 0
1 Title 1 comment 2 1
2 Title 2 comment 3 0
2 …Run Code Online (Sandbox Code Playgroud)