2017-06-16 44 views
0

来自postgres noob的问题。我在postgres中有两张桌子。请参阅下面的原理图。postgres select从不同的表中选择阵列中的id

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 

从蟒编辑,我会分配所述第一查询,以变量的输出,然后检查在第二查询该变量的成员资格。但是,这可能不是这里最好的方式,我希望有一种方法可以将这个功能作为单个查询使用?

编辑

@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

请仔细阅读http://meta.stackoverflow.com/questions/285551/why-may-i-不要上传代码的时候提出问题/ 285557和接受的答案 –

+0

你可以使用子查询。 – Sumit

回答

1

试试这个:

select * 
from variants 
where id in (
    select unnest(variants_id) 
    from strains_snps 
    where strains_snps.name = 'foo' 
) 
+0

作品一种享受!非常感谢! – flashton