从Postgres中选择行,其中列匹配数组中的任何字符串

时间:2016-04-06 17:56:37

标签: r postgresql rpostgresql

这是基本的,如果我错过了现有的答案,我会道歉。我试图在数据库中选择行userid匹配我提供的数组中的任何字符串。以下是我尝试过的方法:

query_string = "SELECT * FROM data WHERE userid = ANY([qFupLOnc1,bUVMBxum0,FlYYzQz31]) LIMIT 5"
query_string = "SELECT * FROM data WHERE userid = ANY(['qFupLOnc1','b2VMBxum0','FlYYzQz31']) LIMIT 5"
query_string = "SELECT * FROM data WHERE userid = ANY(ARRAY['qFupLOnc1','bUVMBxum0','FlYYzQz31']) LIMIT 5"
query_string = "SELECT * FROM data WHERE userid = ANY(ARRAY[qFupLOnc1,bUVMBxum0,FlYYzQz31]) LIMIT 5"
query_string = "SELECT * FROM data WHERE userid ANY(ARRAY[qFupLOnc1,bUVMBxum0,FlYYzQz31]) LIMIT 5"
query_string = "SELECT * FROM data WHERE userid ANY(ARRAY['qFupLOnc1','bUVMBxum0','FlYYzQz31']) LIMIT 5"
query_string = "SELECT * FROM data WHERE userid ANY(['qFupLOnc1','bUVMBxum0','FlYYzQz31']) LIMIT 5"
query_string = "SELECT * FROM data WHERE userid ANY([qFupLIOnc1,bU2VMBxum0,FlYYzQz31]) LIMIT 5"

这些都带来了错误。这样做的正确方法是什么?

我通过R包RPostgreSQL查询数据库,如下所示:

data = dbGetQuery(con, query_string) 

2 个答案:

答案 0 :(得分:0)

试,

 query_string = "SELECT * FROM data WHERE userid like '%foo%'";

答案 1 :(得分:-1)

您想要IN

SELECT * FROM data WHERE userid IN ('...', '...', '...');