2017-06-16 51 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

相關問題