K
K
kolya_krut2019-04-16 15:06:20
PostgreSQL
kolya_krut, 2019-04-16 15:06:20

How to get key by value in JSONB?

The jsonb field contains the following structure:

{
"1552461856.244": 13,
"1552461716.1383": 0,
"1552461717.0405": 3,
"1552462001.1221": 4,
"1552462003.4729": 1,
"1552702001.1221": 4
}

How to get max key for value "4" via SQL? For this example, 1552702001.1221.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Roman Mirilaczvili, 2019-04-16
@2ord

It is wrong to use jsonb when you need to find a key by value. This is the wrong approach.
Instead of jsonb, you need to use 2 fields and then you can search for records

select col_key from table
where col_val=4
order by col_key desc
limit 1

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question