Nic*_*erg 11 sql database arrays 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');
Run Code Online (Sandbox Code Playgroud)
基本上我想要做的是在'author'列上的iLike,以确定该数组中是否存在特定的用户ID.显然我不能在该数据类型上使用iLike,所以我需要找到另一种方法.
以下是'authors'数组中的数据示例:
{"(2373,t,f,f,\"2011-08-01 11:57:40.696496 \",/ Pubs/pubs_edit_article.php,\"2011-08-09 15:36:29.281833 \",000128343, A00592,作者,1,尼古拉斯,K.,Kreidberg,\ "\",123456789,T,管理员,A,A,A,0,\ "\") ""(2374,T,F,F,\ "2011-08-01 11:57:40.706617 \",/ Pubs/pubs_edit_article.php,\"2011-08-09 15:36:29.285428 \",000128343,A00592,作者,2,John,D.,Doe ,\"\",234567890,t,IT,A,A,A,0,\"\")","(2381,t,f,f,\"2011-08-09 14:45:14.870418\",000128343,\"2011-08-09 15:36:29.28854 \",000128343,A00592,作者,3,Jane,E,Doe,\"\",345678901,t,Admin,A,A,A, ,\"\")","(2383,t,f,f,\"2011-08-09 15:35:11.845283 \",567890123,\"2011-08-09 15:36:29.291388 \", 000128343,A00592,作者,如图4所示,测试,T,Testerton,\ "\",TestTesterton,F,N/A,A,A,A ,,\"\")"}
我想要做的是查询视图,并找出数组中是否存在字符串'123456789'(即数组中分配给Nicholas Kreidberg的用户ID).我不关心它被分配给哪个用户或它在数组中出现的位置,我需要知道的是,如果'123456789'显示在数组中的任何位置.
一旦我知道如何编写一个确定上述条件是否为真的查询,那么我的应用程序将只执行该查询,如果返回行,它将知道传递给查询的用户id是该发布的作者并相应地继续.
提前感谢您就此主题提供的任何见解.
gsi*_*ems 24
可能这个:
select ...
from ...
where ...
and array_to_string(authors, ', ') like '%123456789%';`
Run Code Online (Sandbox Code Playgroud)
做诀窍?
否则,有unnest功能......
" 数组函数和运算符 "一章有更多详细信息.
该ANY()函数可以为你做的工作:
SELECT * FROM people WHERE '123456789' = ANY(authors);
Run Code Online (Sandbox Code Playgroud)
给定people.authors的类型为text[]。