json - postgresql,选择多个 jsonb_array_elements 工作很奇怪

标签 json postgresql select

我想使用 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/

相关文章:

sql - 帮我重写这个查询

mysql - Sql使用计数和位置显示所有数据

android - Firebase 服务帐户凭据 Json 读取权限被拒绝

java - 带有 JDBC 或 Hibernate 的 PostgreSQL,用于使用 CSV 数据的 Web 应用程序

c# - 如何使用 JsonConverter 在 System.Text.Json.JsonSerializer.Serialize() 中排除属性的序列化

c - 如何转储 PGresult

SQL:如何在考虑时间限制的情况下对各组进行平均

javascript - 在 JavaScript/jQuery 中获取没有重复的列中的所有值

Java gson,解码到 Map<String,String[]> 时出现 ClassCastException

json - Kotlin:将嵌套的JSON对象转换为文字字符串