gre*_*reg 9 regex arrays postgresql
我有一个包含作者数组的列.如何使用~*运算符检查其值是否与给定的正则表达式匹配?
该~*操作需要一个要检查的字符串左侧和正则表达式匹配右侧.文件说ANY操作员显然必须在右侧
`
SELECT '^p' ~* ANY(authors) FROM book;
Run Code Online (Sandbox Code Playgroud)
`
因为PostgreSQL尝试将字符串^p与数组中包含的表达式进行匹配,所以不起作用.
任何的想法?
第一个明显的想法是使用自己的regexp匹配运算符和commuted参数:
create function commuted_regexp_match(text,text) returns bool as
'select $2 ~* $1;'
language sql;
create operator ~!@# (
procedure=commuted_regexp_match(text,text),
leftarg=text, rightarg=text
);
Run Code Online (Sandbox Code Playgroud)
然后你可以像这样使用它:
SELECT '^p' ~!@# ANY(authors) FROM book;
Run Code Online (Sandbox Code Playgroud)
另一种不同的方式来查看它以取消数组并在SQL中表达ANY结构的等价物:
select bool_or(r) from
(select author ~* '^j' as r
from (select unnest(authors) as author from book) s1
) s2;
Run Code Online (Sandbox Code Playgroud)
小智 7
SELECT * FROM book where EXISTS ( SELECT * from unnest(author) as X where x ~* '^p' )
Run Code Online (Sandbox Code Playgroud)