PostgreSQL - 计算 JSON 中的元素

mja*_*zab 10 arrays postgresql json

我有一个名为“log_data”的 JSON 类型列,其中存储的数据格式为[{"key":"test123123","identity":"user@test.it","identity_type":"email"}].

我想计算json中给定键的给定值的记录数:

不起作用

SELECT count (distinct esas_logs.log_id) AS "count" FROM "esas_logs" WHERE log_data->0->>'identity' = 'user@test.it'

[2016-06-30 13:59:18] [42883] ERROR: operator does not exist: json = unknown
  HINT: No operator matches the given name and argument type(s). You might need to add explicit type casts.
Run Code Online (Sandbox Code Playgroud)

Chr*_*ers 15

json_array_length()

test=# select  json_array_length('[{"key":"test123123","identity":"user@test.it","identity_type":"email"}]');
  json_array_length 
 -------------------
             1
 (1 row)
Run Code Online (Sandbox Code Playgroud)

  • 如果该字段设置为“jsonb”,则使用“jsonb_array_length()”方法。 (5认同)