简体   繁体   English

在PostgreSQL列中搜索子字符串

[英]Search PostgreSQL column for substring

I have a DB column that has entries like this: 我有一个包含以下条目的数据库列:

  • "56/45/34" “ 56/45/34”
  • "78/34/145" “ 78/34/145”
  • "45" “ 45”
  • "" (ie NULL) “”(即NULL)

I want to search for the rows that match a certain number - for example "45" would should return the first and third rows but not the second. 我想搜索与某个数字匹配的行-例如,“ 45”应返回第一行和第三行,而不返回第二行。

We can try using a regex approach here with word boundaries: 我们可以在这里尝试使用带有单词边界的正则表达式方法:

select col
from your_table
where col ~* '\y45\y';

在此处输入图片说明

Demo 演示版

You can convert the delimited string to an array and then test the array 您可以将定界字符串转换为数组,然后测试该数组

select *
from the_table
where '45' = any(string_to_array(the_column, '/'))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM