我有这张桌子:
CREATE TABLE lawyer (
id SERIAL PRIMARY KEY,
data jsonb
);
INSERT INTO lawyer (data) VALUES
('{"a": 1}'),
('{"tags":["derecho", "civil", "laboral", "penal"]}'),
('{"tags":["derecho", "penal"]}')
;
我想要的是postgres中的JSONb查询,当我需要找到包含" civil" OR
" derecho"
答案 0 :(得分:2)
终于找到了一种方法:
将json数组直接存储为顶级:
INSERT INTO lawyer (data) VALUES
('["derecho","laboral","penal"]'),
('["derecho", "civil", "laboral", "penal"]'),
('["derecho", "penal"]')
;
SELECT *
FROM lawyer
WHERE data ? 'penal';
结果:
答案 1 :(得分:2)
对于那些寻找原始数据结构答案的人来说,这里是SQL:
SELECT * FROM lawyer WHERE lawyer.data->'tags' ? 'penal'