I want to select the last value from an array nested like this:
{ tier: [
{ tier: [] },
{ tier: [] },
{ tier: [
{ tier: 1},
{ tier: 2},
{ tier: 3}, // < this item
]
},
]
}
I've tried to use something like this from other examples, but the syntax is eluding me. I either get a 'column does not exist' or 'cannot extract elements from object'
SELECT *,
t0->tier->(jsonb_array_length(t0->tier) - 1) t1,
t1->tier->(jsonb_array_length(t1->tier) - 1) t2,
t2->tier->(jsonb_array_length(t2->tier) - 1) t3,
FROM data t0
You can reference it by negative index:
SELECT t -> 'tier' -> -1 -> 'tier' -> -1
FROM data;
The -1 here references the last item of the array, which means you don't need to calculate how many elements there are.
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.