首页 > 解决方案 > 检查postgresql中逗号分隔列中的逗号分隔字符串

问题描述

我有一个字符串searchval = "php,java,html"

我想检查下表中列关键字中是否存在字符串中的任何逗号分隔值。

我的桌子

id     keyword
-----------------------------
1      java,php
2      jquery, javascript
3      java
4      php,jquery

任何与关键字(逗号分隔)匹配的 searchval(逗号分隔)值都应返回 id。

所以结果应该是:

1 (java,php is there),
3 (java is there),
4 (php is there)

我怎样才能做到这一点?

标签: sqlpostgresql

解决方案


您可以轻松地将逗号分隔的列表转换为数组,然后使用 Postgres 的数组函数来测试是否包含:

select id
from the_table
where string_to_array(keyword,',') && array['php','java','html'];

重叠运算符&&检查从关键字列表创建的数组是否包含右侧数组中的元素。

在线示例:http ://rextester.com/VNWP17762


推荐阅读