我有一个如下所示的查询,
select * from tbl1 where field1 in (select field2 from tbl2 where id=1)
- 其中 field1 的类型为整数
- 和
select field2 from tbl2 where id=1
将返回“1,2,3”,这是一个字符串
显然,我们不能传递字符串来检查整数类型的字段。我想问的是,如何解决上述查询?或者有没有其他解决方案?
我的数据库管理系统:Postgresql 9.0.3
最佳答案
select *
from tbl1
where field1 in (
select regexp_split_to_table(field2, ',')::int
from tbl2
where id=1
)
伊戈尔建议的变体
select *
from tbl1
where field1 = any (
select regexp_split_to_array(field2, ',')::int[]
from tbl2
where id=1
)
关于sql - 在Where in () 中传递查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19227459/