我想使用 json_array_elements 来扩展 json 数组。但它的工作方式很奇怪。请看下面。
select json_array_elements('[1, 2]') as a, json_array_elements('[2, 3, 4]') as b;
a | b
---+---
1 | 2
2 | 3
1 | 4
2 | 2
1 | 3
2 | 4
(6 行)
select json_array_elements('[1, 2]') as a, json_array_elements('[2, 3]') as b;
a | b
---+---
1 | 2
2 | 3
(2 行)
似乎当数组的长度相等时,出现了问题。 谁能告诉我,为什么会这样。
最佳答案
PostgreSQL 重复每个列表,直到它们恰好同时位于末尾。
也就是说,结果列表的长度是least common multiple输入列表的长度。
这种行为确实很奇怪,将在 PostgreSQL v10 中改变:
select json_array_elements('[1, 2]') as a, json_array_elements('[2, 3, 4]') as b;
a | b
---+---
1 | 2
2 | 3
| 4
(3 rows)
来自commit message :
While moving SRF evaluation to ProjectSet would allow to retain the old "least common multiple" behavior when multiple SRFs are present in one targetlist (i.e. continue returning rows until all SRFs are at the end of their input at the same time), we decided to instead only return rows till all SRFs are exhausted, returning NULL for already exhausted ones. We deemed the previous behavior to be too confusing, unexpected and actually not particularly useful.
关于json - postgresql,选择多个 jsonb_array_elements 工作很奇怪,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42196156/