来自postgres noob的问题。我在postgres有两张桌子。请参见下面的原理图。
strains_snps::table
name::str
variants_id::int array
variants::table
id::int
ref_base::str
我想
select variants_id
from strains_snps
where strains_snps.name = 'foo'
然后在后续查询中使用variants_id(这是一个int数组)。
select *
from variants
where id in the_output_from_previous_query
来自python,我会将第一个查询的输出分配给变量,然后在第二个查询中检查该变量的成员身份。但这可能不是最好的方式,我希望有一些方法可以让它作为单个查询工作?
修改
@Sumit建议使用子查询。我试过这个,但没有成功。
select * from variants
where id in
(select variants_id from strains_snps where name = 'BMD1964')
返回的错误pgadmin是
ERROR: operator does not exist: integer = integer[]
LINE 2: where id in
^
HINT: No operator matches the given name and argument type(s). You
might need to add explicit type casts.
********** Error **********
ERROR: operator does not exist: integer = integer[]
SQL state: 42883
Hint: No operator matches the given name and argument type(s). You might need to add explicit type casts.
Character: 34
答案 0 :(得分:4)
试试这个:
select *
from variants
where id in (
select unnest(variants_id)
from strains_snps
where strains_snps.name = 'foo'
)
答案 1 :(得分:0)
如果要使用WITH语句将其分隔:
WITH ids AS (
SELECT UNNEST(variants_id)
FROM strains_snps
WHERE strains_snps.name = 'foo' )
SELECT *
FROM variants
WHERE id IN (SELECT * FROM ids);