2017-08-28 26 views
1

我發現大多數的你追問這個tread,但我有問題可以得到正確的位出我的查詢,jsonb搜索,只顯示規格值

的jsonb列如下:

[ 
{"price": 67587, "timestamp": "2016-02-11T06:51:30.696427Z"}, 
{"price": 33964, "timestamp": "2016-02-14T06:49:25.381834Z"}, 
{"price": 58385, "timestamp": "2016-02-19T06:57:05.819455Z"}, etc.. 
] 

查詢看起來是這樣的:

SELECT * FROM store_product_history 
WHERE EXISTS (SELECT 1 FROM jsonb_array_elements(store_prices) 
as j(data) WHERE (data#>> '{price}') LIKE '%236%'); 

這當然給了我完整行的結果,但我想只得到像從行唯一的時間戳值,我這可能嗎?

回答

0

如果在橫向連接中使用jsonb_array_elements(),您將能夠選擇單個json屬性,例如,

with store_product_history(store_prices) as (
values 
('[ 
    {"price": 67587, "timestamp": "2016-02-11T06:51:30.696427Z"}, 
    {"price": 33964, "timestamp": "2016-02-14T06:49:25.381834Z"}, 
    {"price": 58385, "timestamp": "2016-02-19T06:57:05.819455Z"} 
]'::jsonb) 
) 

select data 
from store_product_history, 
jsonb_array_elements(store_prices) as j(data) 
where (data#>> '{price}') like '%6%'; 

          data        
-------------------------------------------------------------- 
{"price": 67587, "timestamp": "2016-02-11T06:51:30.696427Z"} 
{"price": 33964, "timestamp": "2016-02-14T06:49:25.381834Z"} 
(2 rows) 

或者:

select data->>'timestamp' as timestamp 
from store_product_history, 
jsonb_array_elements(store_prices) as j(data) 
where (data#>> '{price}') like '%6%'; 

      timestamp   
----------------------------- 
2016-02-11T06:51:30.696427Z 
2016-02-14T06:49:25.381834Z 
(2 rows)