Substring search on JSON data (ERROR: cannot extract element from a scalar)
Substring search on JSON data (ERROR: cannot extract element from a scalar)
我一直在寻找 this tutorial and this 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" } ');
我正在寻找一种方法来仅搜索 "text_entry" 和 return 任何具有子字符串 "the Moon" 的情况(在这种情况下,它将是 id = 1 & 2).预计 Return:
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 中的子字符串吗?
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)
我一直在寻找 this tutorial and this 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" } ');
我正在寻找一种方法来仅搜索 "text_entry" 和 return 任何具有子字符串 "the Moon" 的情况(在这种情况下,它将是 id = 1 & 2).预计 Return:
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 中的子字符串吗?
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)