在PostgreSQL中的数组列中查找字符串
我在PostgreSQL数据库中构建了一系列视图,其中包含几个数组列.视图定义如下:
create view articles_view as (select articles.*,array(select row(people.*)::people from people where articles.spubid=people.spubid and people.stype='Author' and bactive='t' order by people.iorder) as authors,array(select row(people.*)::people from people where articles.spubid=people.spubid and people.stype='Editor' and bactive='t' order by people.iorder) as editors,array(select row(people.*)::people from people where articles.spubid=people.spubid and people.stype='Reviewer' and bactive='t' order by people.iorder) as reviewers,array(select row(status.*)::status from status where articles.spubid=status.spubid and bactive='t') as status from articles where articles.bactive='t'); 基本上我想做的是在’author’列上的iLike,以确定该数组中是否存在特定的用户ID.显然我不能在该数据类型上使用iLike,所以我需要找到另一种方法. 以下是’authors’数组中的数据示例:
我想要做的是查询视图并找出数组中是否存在字符串’123456789′(即数组中分配给Nicholas Kreidberg的用户ID).我不关心它被分配给哪个用户或它在数组中出现的位置,我需要知道的是,如果’123456789’出现在数组中的任何位置. 一旦我知道如何编写一个确定上述条件是否为真的查询,那么我的应用程序将只执行该查询,如果返回行,它将知道传递给查询的用户id是该发布的作者并相应地继续. 提前感谢您就此主题提供的任何见解.
可能这个:
select ... from ... where ... and array_to_string(authors,',') like '%123456789%';` 这个伎俩? 否则,有不必要的功能…… “Array Functions and Operators”章节有更多细节. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |