Reputation: 11696
I have a table to store information about my rabbits. It looks like this:
create table rabbits (rabbit_id bigserial primary key, info json not null);
insert into rabbits (info) values
('{"name":"Henry", "food":["lettuce","carrots"]}'),
('{"name":"Herald","food":["carrots","zucchini"]}'),
('{"name":"Helen", "food":["lettuce","cheese"]}');
How should I find the rabbits who like carrots? I came up with this:
select info->>'name' from rabbits where exists (
select 1 from json_array_elements(info->'food') as food
where food::text = '"carrots"'
);
I don't like that query. It's a mess.
As a full-time rabbit-keeper, I don't have time to change my database schema. I just want to properly feed my rabbits. Is there a more readable way to do that query?
Upvotes: 272
Views: 319902
Reputation: 360
You can use the POSITION() function or the LIKE operator with wildcard characters.
SELECT * FROM your_table WHERE (POSITION( "Feild1" IN "Feild2") > 0) ;
or
SELECT * FROM your_table WHERE "Feild2" like '%' || "Feild1" || '%';
Upvotes: 0
Reputation: 701
In order to select the specific key in the JSONB, you should use ->.
select * from rabbits where (info->'food')::jsonb ? 'carrots';
Upvotes: 8
Reputation: 586
You can do a direct type cast from jsonb to text incase you want to check the full json and not one key.
select * from table_name
where
column_name::text ilike '%Something%';
Upvotes: 13
Reputation: 10296
SELECT a.crops ->> 'contentFile' as contentFile
FROM ( SELECT json_array_elements('[
{
"cropId": 23,
"contentFile": "/menu/wheat"
},
{
"cropId": 25,
"contentFile": "/menu/rice"
}
]') as crops ) a
WHERE a.crops ->> 'cropId' = '23';
OUTPUT:
/menu/wheat
Upvotes: 1
Reputation: 341
If the array is at the root of the jsonb column, i.e. column looks like:
food |
---|
["lettuce", "carrots"] |
["carrots", "zucchini"] |
just use the column name directly inside the brackets:
select * from rabbits where (food)::jsonb ? 'carrots';
Upvotes: 16
Reputation: 785
Not simpler but smarter:
select json_path_query(info, '$ ? (@.food[*] == "carrots")') from rabbits
Upvotes: 2
Reputation: 11696
As of PostgreSQL 9.4, you can use the ?
operator:
select info->>'name' from rabbits where (info->'food')::jsonb ? 'carrots';
You can even index the ?
query on the "food"
key if you switch to the jsonb type instead:
alter table rabbits alter info type jsonb using info::jsonb;
create index on rabbits using gin ((info->'food'));
select info->>'name' from rabbits where info->'food' ? 'carrots';
Of course, you probably don't have time for that as a full-time rabbit keeper.
Update: Here's a demonstration of the performance improvements on a table of 1,000,000 rabbits where each rabbit likes two foods and 10% of them like carrots:
d=# -- Postgres 9.3 solution
d=# explain analyze select info->>'name' from rabbits where exists (
d(# select 1 from json_array_elements(info->'food') as food
d(# where food::text = '"carrots"'
d(# );
Execution time: 3084.927 ms
d=# -- Postgres 9.4+ solution
d=# explain analyze select info->'name' from rabbits where (info->'food')::jsonb ? 'carrots';
Execution time: 1255.501 ms
d=# alter table rabbits alter info type jsonb using info::jsonb;
d=# explain analyze select info->'name' from rabbits where info->'food' ? 'carrots';
Execution time: 465.919 ms
d=# create index on rabbits using gin ((info->'food'));
d=# explain analyze select info->'name' from rabbits where info->'food' ? 'carrots';
Execution time: 256.478 ms
Upvotes: 368
Reputation: 5220
A small variation but nothing new infact. It's really missing a feature...
select info->>'name' from rabbits
where '"carrots"' = ANY (ARRAY(
select * from json_array_elements(info->'food'))::text[]);
Upvotes: 20
Reputation: 1258
You could use @> operator to do this something like
SELECT info->>'name'
FROM rabbits
WHERE info->'food' @> '"carrots"';
Upvotes: 69
Reputation: 1445
Not smarter but simpler:
select info->>'name' from rabbits WHERE info->>'food' LIKE '%"carrots"%';
Upvotes: 31