order by JSON data type postgres

后端 未结 3 1009
甜味超标
甜味超标 2021-02-03 23:07

I have a Postgres table which has column of type JSON which has a bunch of JSON objects in them. I want to query the table records and order the results by a value stored in the

相关标签:
3条回答
  • 2021-02-03 23:50

    You put asc in the fieldname. There's no key named value asc in the json, so data ->> 'value asc' will always return NULL.

    You actually want:

    select * from table ORDER BY data->>'value' ASC 
    

    to match the json, possibly even:

    select * 
    from table 
    WHERE data ->> 'name' = 'stuff'
    ORDER BY data->>'value' ASC 
    
    0 讨论(0)
  • 2021-02-03 23:53

    Use -> instead of ->> (->> gets a JSON object field as text):

    select * from my_table ORDER BY data->'some_number' asc; 
    
    0 讨论(0)
  • 2021-02-03 23:53

    Try:

    ORDER BY cast(data->>'value' as integer) ASC

    0 讨论(0)
提交回复
热议问题