Table
CREATE TABLE users
(
username VARCHAR(128) PRIMARY KEY,
info JSONB
);
INSERT INTO users (username, info)
VALUES
('Lana', '[
{
"id": "first"
},
{
"id": "second"
}
]'),
('Andy', '[
{
"id": "first"
},
{
"id": "third"
}
]');
So I want to find all users, whose info.id
contained in array like ["first"].
request should be like:
SELECT *
FROM users
where jsonb_path_exists(info, '$.id ? (@ in ("first", "second", "third",...) )');
But I can’t find the correct implementation
2
Answers
There is no
IN
operator in the SQL/JSON Path language. If you want to find one of many alternatives, use a regex, e.g.You need to iterate over the array elements using
$[*]
then use==
Or maybe collect all IDs and use the
?|
operator:That would return rows that contain at least one of those values.
If you need to find rows that contain all values, use
?&
instead.