我有一個表accounts,該表將多個帳戶首選項存盤為其preferences列中帶有鍵/值對的散列。
preferences 列以下列方式存盤資料:
{
"accepts_coupons": true,
"accepted_coupon_types": ["percentage", "amount"],
"some_other_preference": [...],
}
如何選擇accepted_coupon_types內容包含percentage值的帳戶?
例如 (偽代碼) select * from accounts where accounts.preferences.some_key in ('some_value')?
我試過這樣的事情:
select name, preferences from accounts
where preferences in ('percentage')
但我得到 ERROR: invalid input syntax for type json, Detail: Token "percentage" is invalid.
是否可以撰寫一個查詢來查看列鍵/值的內部,以在其首選項列中選擇滿足特定值條件的帳戶?
uj5u.com熱心網友回復:
這樣的事情應該作業:
select name, preferences
from accounts
where preferences ->> 'accepted_coupon_types' like '%percentage%'
db-fiddle 在這里:https : //www.db-fiddle.com/f/cBsWdNZnmEjuaeZXT1yWju/0
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/313921.html
標籤:sql PostgreSQL
