Answer the question
In order to leave comments, you need to log in
How to do a join or SELECT FROM WHERE IN from an array?
There are two tables. tokens and regions. The tones table has a region_id in the form of a json array
Returns the following result https://imgur.com/ZdtIlYz
https://imgur.com/5fY8Lvf
I need to extract all regions from the regions table. Tried like this:
SELECT
"ru_name"
FROM "public"."regions"
WHERE
"id" in ( SELECT json_array_elements(regions) FROM "tokens" where user_id = 5 );
Answer the question
In order to leave comments, you need to log in
SELECT
ru_name
FROM "tokens"
JOIN regions ON regions.id = any (tokens.regions)
WHERE user_id = 5 ;
with reg_ids as (
select unnest(regions) reg_id
from tokens where user_id = 5
) select regions.*
from reg_ids
join regions on regions.id = reg_id;
select ... from regions
where id in (
select j::int
from tokens cross join json_array_elements_text(regions) as j
where user_id = 5
);
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question