【问题标题】:how to get the particular index(n th position) element from a list of json values in postgres sql如何从postgres sql中的json值列表中获取特定的索引(第n个位置)元素
【发布时间】:2020-09-18 08:15:49
【问题描述】:

这个我的 json 对象已经存储在我的 postgres 11 数据库中

[{“user_id”:0,“user_name”:“”,“user_role”:“”,“start_timestamp”: “2020-09-08 04:01:31.636848”,“end_timestamp”:“2020-09-08 04:01:31.636848”,“hold_timestamp_list”:[],“handover_link”:“”, “curr_state”:“待分配”,“is_complete”:1,“is_onhold”:0}, {“user_id”:910,“user_name”:“INM”,“user_role”:“”, “start_timestamp”:“2020-09-09 05:11:06.476766”,“end_timestamp”:“”, "hold_timestamp_list": [{"s": "2020-09-09 05:11:07.359749", "e": ""}], “handover_link”:“”,“curr_state”:“创作”,“is_complete”:0, "is_onhold": 1}]

如何从最后一个索引中获取 user_name(即:索引 2,值 =

{“user_id”:910,“user_name”:“INM”,“user_role”:“”, “start_timestamp”:“2020-09-09 05:11:06.476766”,“end_timestamp”:“”, "hold_timestamp_list": [{"s": "2020-09-09 05:11:07.359749", "e": ""}], “handover_link”:“”,“curr_state”:“创作”,“is_complete”:0, “is_onhold”:1}

【问题讨论】:

  • 您使用的是哪个 Postgres 版本?

标签: sql arrays json postgresql


【解决方案1】:

您可以使用负索引来获取 json 数组的最后一个元素,如下所示:

mycol -> -1

如果要对应的用户名:

mycol -> -1 ->> 'user_id'

或者你可能想要一个整数结果:

(mycol -> -1 ->> 'user_id')::int

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-13
    • 1970-01-01
    相关资源
    最近更新 更多