这里有两种使用json_array_elements_text()
方法的方法。
第一种方法:
SELECT id, json_array_elements_text(json_column)
FROM Table
第二种方法:
SELECT id, json_index.json_column
FROM Table T
LEFT JOIN LATERAL json_array_elements_text(json_column) AS json_index(json_column) ON true
我发现这两种方法给出相同的输出,但是我们可以互换使用它们吗?
.
最佳答案
这两个查询碰巧产生相同的结果。使用 LATERAL
连接的查询是执行此操作的显式(并且在我看来更好)的方法。
The documentation描述第一种情况:
Functions returning sets can also be called in the select list of a query. For each row that the query generates by itself, the set-returning function is invoked, and an output row is generated for each element of the function's result set.
[...]
PostgreSQL's behavior for a set-returning function in a query's select list is almost exactly the same as if the set-returning function had been written in a
LATERAL FROM
-clause item instead.
文档随后描述了细微的差异,但它们在您的情况下应该不重要。
关于sql - 在 PostgreSQL 中使用 Jsonb_array_elements_text() 的 2 种方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67571698/