Reputation: 45
I have JSON column like this
data (column name)
row 1 [58,96,36,196,25,23]
row 2 [26,654,269,22,96]
how to write a query
$user = 96
;
how to search $user
using PostgreSQL database, how do we write a query using PostgreSQL database, based on $user
search entire columns of the table
Upvotes: 1
Views: 98
Reputation:
Assuming you are using jsonb
(which is highly recommended) you can use the contains operator @>
:
select *
from the_table
where data @> '96'::jsonb
If you are using Postgres 12, you can also use the new JSONB path expression:
select *
from the_table
where data @? '$[*] ? (@ == 96)'
Upvotes: 0
Reputation: 1733
Use JSON functions, something like:
with table_with_json as (
select '[58,96,36,196,25,23]'::json json_column union all
select '[26,654,269,22,96]'::json
)
select
twj.json_column
from
table_with_json twj
where
exists (select t.value from json_array_elements(twj.json_column) t where t.value::text = $user::text);
Upvotes: 0