gpt4 book ai didi

sql - 如何在 where 子句中使用正则表达式来过滤 Postgres 中的行?

转载 作者:行者123 更新时间:2023-12-04 17:27:59 25 4
gpt4 key购买 nike

我在 postgres 11.0 中有下表

<表类="s-表"><头>col1col2col3col4<正文>NCT000017234894402赛尼可(奥利司他)胶囊赛尼可NCT000017244894403赛尼可(奥利司他)胶囊奥利司他NCT000017254894404胶囊胶囊NCT000017264894405胰岛素插入

我想过滤以上行,以便 col3 = col4 或 col3 确切内容应包含在 col4 中。

期望的输出是:

<表类="s-表"><头>col1col2col3col4<正文>NCT000017234894402赛尼可(奥利司他)胶囊赛尼可NCT000017244894403赛尼可(奥利司他)胶囊奥利司他NCT000017254894404胶囊胶囊

我正在尝试通过以下查询获取此输出。

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

这里的任何建议都会很有帮助。谢谢

最佳答案

如果我没听错,你可以使用 word boundaries escape \y :

\y: matches only at the beginning or end of a word

在您的查询中:

select * from mytable where col3 ~  ('\y' || col4 || '\y')

Demo on DB Fiddle :

<表类="s-表"><头>col1col2col3col4<正文>NCT000017234894402赛尼可(奥利司他)胶囊赛尼可NCT000017244894403赛尼可(奥利司他)胶囊orlistatNCT000017254894404胶囊胶囊

关于sql - 如何在 where 子句中使用正则表达式来过滤 Postgres 中的行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62142745/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com