简体   繁体   中英

Select “WHERE IN” with PostgreSQL and JSONB

Given table_a like this:

 id | name
----+------
  1 | aaaa
  2 | bbbb
  3 | cccc

I can obviously issue the following query:

SELECT * FROM table_a WHERE name IN ('aaaa', 'bbb');

But given table_b like this:

 id |       data
----+------------------
  1 | {"name": "aaaa"}
  2 | {"name": "bbbb"}
  3 | {"name": "cccc"}

How do I issue a query "give me all the rows where the value of the key name is contained in this list of values?"

I know I can use the jsonb operator @> to check for each combination, but unfortunately I'd have to issue as many queries as the number of values I want to check against. Is there a way to do it in one query ?

UPDATE:

I found a solution right away:

select * from table_b where data #>> '{name}' IN ('aaaa', 'bbb');
SELECT * FROM table_a WHERE data->>'name' IN ('aaaa', 'bbbb')

好像是你想要的吗?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM