2017-03-14 38 views
0

我一直在尋找this tutorialthis example試圖找到一種高性能的方式通過JSON(< 9.5)/ JSONB串查詢(* 9.5)數據。字符串搜索(錯誤:不能從一個標量提取元素)

例如,我有這樣的數據:

CREATE TABLE public.foo 
(
    row_id SERIAL PRIMARY KEY, 
    data json 
); 

INSERT INTO foo VALUES (1, 
    '{ "name": "Book the First", "author": "Bob", "text_entry": "White Cow Walked Over the Moon" } '); 
INSERT INTO foo VALUES (2, 
    '{ "name": "Book the Second", "author": "Charles", "text_entry": "Humptey Dumptey sat on the Moon" } '); 
INSERT INTO foo VALUES (3, 
    '{ "name": "Book the Third", "author": "Jim", "text_entry": "Red Fox jumped over Brown Dog" } '); 

我正在尋找一種方法來搜索ONLY「text_entry」,並返回具有子串「月亮」(在這種情況下,任何情況下, ,它會是id = 1 & 2)。預期收益:

text_entry 
"White Cow Walked Over the Moon" ## has the substring "the Moon" 
"Humptey Dumptey sat on the Moon" ## has the substring "the Moon" 

到目前爲止,我的查詢看起來是這樣的:

SELECT data->'text_entry'->'%the Moon%' AS query FROM foo; 

ERROR: cannot extract element from a scalar 
********** Error ********** 

有沒有來查詢JSON/B子任何優雅的方式?

回答

0

I'm looking for a way to search ONLY "text_entry" and return any case that has the sub-string "the Moon"

SELECT data->>'text_entry' as query 
FROM foo 
WHERE data->>'text_entry' LIKE '%the Moon%'; 

- >

query    
--------------------------------- 
White Cow Walked Over the Moon 
Humptey Dumptey sat on the Moon 
(2 rows) 
相關問題