[英]Rails Postgres - Jsonb Column Query
我有一個帶有名為“available_quantity”的 jsonb 列的捆綁表。 這將有樣本值
[{“面額”:“100”,“數量”:“20”},{“面額”:“1000”,“數量”:“19”}]
現在,我想查詢所有數量小於 50 的記錄。
我試過這個查詢,
Bundle.where("(available_quantity->>'quantity')::numeric < 50")
但是這返回空的關系。
我怎樣才能做到這一點?
Table "offer_service.bundles"
Column | Type | Collation |
Nullable | Default
-----------------------+-----------------------------+-----------+----
------+--------------------------------------------------------
id | bigint | | not
null | nextval('offer_service.bundles_id_seq'::regclass)
project_id | bigint | |
|
item_type | character varying | |
|
item_id | bigint | |
|
status | integer | | not
null | 0
created_at | timestamp without time zone | | not
null |
updated_at | timestamp without time zone | | not
null |
denomination_quantity | jsonb | |
|
deleted_at | timestamp without time zone | |
|
available_quantity | jsonb | |
|
示例數據:
id: 2586, project_id: 3, item_type: "GiftCard", item_id: 659, status:
"activated", created_at: "2020-05-18 09:38:54", updated_at: "2020-05-
28 13:25:29", denomination_quantity: {"100"=>200, "1000"=>200},
deleted_at: nil, available_quantity: [{"quantity"=>16,
"denomination"=>"100"}, {"quantity"=>20, "denomination"=>1000}]
希望我們必須在這里使用子查詢。
Bundle.select("*").from(Bundle.select("*, jsonb_array_elements(available_quantity) as aq")).where("(aq ->> 'quantity')::numeric <= 50").distinct
歡迎提出重構上述查詢的建議!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.