我有一個 PostgresSQL 表,如下所示:
id order_id products
[PK] integer integer character varying
1 123 {"type": "foo", "counts": 2}
2 456 {"type": "foobar", "counts": 4}
3 789 {"type": "foo", "counts": 1}
4 678 {"type": "baz", "counts": 3}
我只想查詢 where type = foo。
在另一個查詢中,我已成功使用以下內容:
SELECT
table_a.data::json->>'type' prod_type,
FROM table.a
但是,這僅適用于該data列是 JSON 型別。
我將如何索引到products列中以便我只回傳type = foo?
1 123 {"type": "foo", "counts": 2}
3 789 {"type": "foo", "counts": 1}
謝謝!
uj5u.com熱心網友回復:
你可以嘗試這樣的事情:
SELECT *
FROM test
WHERE products::json->>'type' = 'foo';
樣本輸出:

轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/478875.html
標籤:sql PostgreSQL
