检查数组中的子字符串是否出现在字符串中?

avi*_*avi 2 sql postgresql

我有以下查询:

select case when count(*)>0 then true else false end
from tab
where param in ('a','b') and position('T' in listofitem)>0
Run Code Online (Sandbox Code Playgroud)

这将检查'T'列中是否存在,listofitem如果计数是> 0.基本上它是搜索子字符串.

这在私人案件中运作良好.不过我的真实情况是我text[]调用了sub_array多个值进行检查.如何修改查询以处理sub_array类型?我更喜欢在查询而不是带LOOP的函数中使用它.

我真正需要的是:

select case when count(*)>0 then true else false end
from tab
where param in ('a','b') and position(sub_array in listofitem)>0
Run Code Online (Sandbox Code Playgroud)

这是因为sub_array 类型不起作用Text[]

poz*_*ozs 6

使用该unnest()函数扩展您的数组&bool_and()(或bool_or()- 这取决于您想要匹配的内容:所有数组元素,或至少一个)来聚合:

select count(*) > 0
from   tab
where  param in ('a','b')
and    (select bool_and(position(u in listofitem) > 0)
        from   unnest(sub_array) u)
Run Code Online (Sandbox Code Playgroud)