[英]How to use regexp in where clause to filter rows in Postgres?
我在 postgres 11.0 中有下表
col1 col2 col3 col4
NCT00001723 4894402 xenical (orlistat)capsules xenical
NCT00001724 4894403 xenical (orlistat)capsules orlistat
NCT00001725 4894404 capsules capsules
NCT00001726 4894405 insulin ins
我想在行上方进行过滤,以便 col3 = col4 或 col3 的确切内容应包含在 col4 中。
所需的 output 是:
col1 col2 col3 col4
NCT00001723 4894402 xenical (orlistat)capsules xenical
NCT00001724 4894403 xenical (orlistat)capsules orlistat
NCT00001725 4894404 capsules capsules
我正在尝试以下查询以获取此 output。
SELECT
*
FROM
table
where col3 = col4 or --exact match
regexp_matches(col3, '(.*).*\(') = col4 or --match content before brackets
regexp_matches(col3, '.*\(.*\).*') = col4 --match content in brackets
这里的任何建议都会很有帮助。 谢谢
如果我正确地跟着你,你可以只使用单词边界转义\y
:
\y
:仅匹配单词的开头或结尾
在您的查询中:
select * from mytable where col3 ~ ('\y' || col4 || '\y')
col1 | col2 | col3 | col4 :---------- | ------: | :------------------------- | :------- NCT00001723 | 4894402 | xenical (orlistat)capsules | xenical NCT00001724 | 4894403 | xenical (orlistat)capsules | orlistat NCT00001725 | 4894404 | capsules | capsules
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.