简体   繁体   中英

Handling a Json column on Postgresql

[please upvote the question if you see that I put effort searching and formulating it. This will surely help!]

What is the difference between this code:

SELECT id, json_array_elements_text(json_column)
FROM Table

And this one:

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

I found they give the same output, but can we use them interchangeably?

These two queries happen to produce the same result. The query with the LATERAL join is the explicit (and, in my opinion, better) way to do it.

The documentation describes the first case:

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.

The documentation then goes on to describe the subtle differences, but they shouldn't matter in your case.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM