sql - 整个短语的Postgres位置功能?字符串搜索
问题描述
有没有办法使用位置或其他函数在字段中查找字符串但不让它成为另一个单词的一部分?例如,如果我正在寻找带有位置的“长凳”这个词,我会做这样的事情
POSITION('bench' IN products)
产品列看起来像这样
**Products**
tool bench
benchpress
park bench
benchmark
bench heater
utility bench tool
我只想匹配 bench 不是另一个单词的一部分的行(不想匹配 benchpress 或 benchmark)。谢谢
解决方案
使用\y
正则表达式(单词边界匹配器):
with t(word) as (values
('tool bench'),
('benchpress'),
('park bench'),
('benchmark'),
('bench heater'),
('utility bench tool')
)
select *
from t
where word ~ '\ybench\y'
推荐阅读
- javascript - 如何展平不同对象的嵌套数组但仅从现有属性名称中获取值?
- bash - 代码没有列出两个输入之间的所有偶数
- javascript - Html 和 Javascript:结合按钮和 js 功能代码
- reactjs - 根据从 reactjs 中的下拉选项中选择来渲染对象
- r - R:更改文件夹中所有文件的文本
- windows-server-2008 - PowerShell 5.0 PathTooLongException 错误
- java - minimax tic-tac-toe 算法总是错误地将“X”或“O”放在下一个可用位置
- php - 如果列被另一个表引用,防止行删除 PHP | 拉拉维尔 | mysql
- c# - 如何读取 8 位图像文件?
- c# - 使用 C# RSACryptoServiceProvider 类生成 SSH Putty 私有对