【问题标题】:PSQL select key of the first elemen of JSON fieldJSON字段第一个元素的PSQL选择键
【发布时间】:2016-03-18 15:57:56
【问题描述】:

我有一个包含 JSON 数据类型字段的表。我想选择这个 JSON 字段中存在的值:

dev=> select id, command from statemachine_history;

 id  |                                       command                                        
------+--------------------------------------------------------------------------------------
    1 | {"Common\\Accounting\\StateMachine\\Command\\CreateDebitorCommand":true}
    2 | []
    3 | {"Common\\Message\\StateMachine\\MessageSendOutCommand":true}
    4 | []
    5 | {"Common\\Message\\StateMachine\\MessageSendOutCommand":false}
    6 | {"Common\\Message\\StateMachine\\MessageCheckRetryCommand":true}
    7 | {"Common\\Newsletter\\Command\\IssueConfirmationRequest":true}
    8 | {"Common\\Customer\\Command\\CustomerActivateCommand":true}
    9 | {"Common\\Customer\\Command\\CustomerRegisterCommand":true}
   10 | {"Common\\Accounting\\StateMachine\\Command\\CreateDebitorCommand":true}
   11 | []
   12 | {"Common\\Message\\StateMachine\\MessageSendOutCommand":true}
   13 | {"Common\\Accounting\\StateMachine\\Command\\CreateDebitorCommand":true}
 1033 | []
   14 | []
   15 | {"Common\\Message\\StateMachine\\MessageSendOutCommand":true}
   16 | {"Common\\Customer\\Command\\CustomerActivateCommand":true}
   17 | {"Common\\Customer\\Command\\CustomerRegisterCommand":true}
   18 | []
   19 | {"Common\\Message\\StateMachine\\MessageSendOutCommand":false}
   20 | {"Common\\Message\\StateMachine\\MessageCheckRetryCommand":true}
   21 | {"INTPASS\\Appointment\\StateMachine\\Command\\FinalizeBookingsCommand":true}
   22 | {"INTPASS\\Appointment\\StateMachine\\Command\\FinalizeBookingsCommand":true}

我想知道如何访问这个数组的键。具体来说,我想从该表中选择命令字段中存在某些值的位置。

我尝试了以下方法,但它不起作用:

SELECT * FROM statemachine_history WHERE command->'key' = 'Common\\Message\\StateMachine\\MessageSendOutCommand'

【问题讨论】:

    标签: sql json database postgresql psql


    【解决方案1】:

    我可以将 JSON 字段数据类型更改为字符串,然后可以使用 LIKE 关键字 %% 示例:

    SELECT identifier FROM statemachine_history WHERE schema_name='Subscription' AND command::varchar LIKE '%ChangeMembershipPlanCommand%'
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-08-17
      • 2018-07-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-10
      相关资源
      最近更新 更多