[英]Extract elements from an array inside a jsonb field in Postgresql
[英]How to extract text from jsonb array in PostgreSQL
我希望能夠從新列中的 jsonb 數組中提取文本作為文本。 這是我的 SQL 表。
ID。 | 錯誤代碼。 |
---|---|
101 | ["exit code: 1"] |
102 | ["exit code: 3"] |
103 | ["OOMKILLED"] |
這是我的列表達式'[]'::jsonb
我需要幫助了解在那種情況下我可以使用哪個 select 命令。 我使用了上面的查詢但沒有成功。
Select reasons -> ' ' as TTT from my_table
我想在 select 命令后獲得這些結果,所以我可以執行 SQL 過濾器,例如where error = 'exit code: 1'
或where error = 'OOMKILLED'
ID。 | 錯誤 |
---|---|
101 | 退出代碼:1 |
102 | 退出代碼:3 |
103 | 被殺 |
試試這個:
SELECT Id, jsonb_array_elements_text(ErrorCode :: jsonb) AS Error
FROM my_table
有關 json 功能的更多信息,請參閱手冊
當您需要檢查 JSONB 數組是否包含某些值時,只需使用? 操作員:
select * from t where err ? 'OOMKILLED';
使用->>
將第一個數組元素提取為文本:
Select id, reasons ->> 0 as reason
from my_table
where reasons ->> 0 in ('exit code: 1','OOMKILLED');
如果您不想重復表達式,請使用派生表:
select *
from (
select id, reasons ->> 0 as reason
from my_table
)
where reason in ('exit code: 1','OOMKILLED');
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.