簡體   English   中英

在 postgres json 數組中查找單個元素

[英]Finding a single element in postgres json array

我有一個具有 Json 類型列的表,我正在嘗試根據該 json 列中包含的值查詢該表。

這是相關的DDL:

create table user
(
    roles json
);

下面是角色值的樣子:

[70,254]

我希望能夠執行這樣的查詢:

select * from user where roles in(254);

所以我試過這個:

SELECT * from apptree.atf_app_user where roles @> 254;

它給了我這個錯誤:

[2017-12-01 14:58:16] [42883] ERROR: operator does not exist: json @> integer
[2017-12-01 14:58:16] Hint: No operator matches the given name and argument type(s). You might need to add explicit type casts.
[2017-12-01 14:58:16] Position: 48

運算符@> 適用於 jsonb 參數。 將列轉換為 jsonb:

select * 
from apptree.atf_app_user 
where roles::jsonb @> '254';

更新。 沒有運算符可以在 json 數組中搜索多個值中的任何一個。 您可以使用函數json_array_elements_text(),例如:

select t.* 
from apptree.atf_app_user t
cross join json_array_elements_text(roles)
where value in ('70', '71');

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM