sql - 检查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)
我怎样才能做到这一点?
解决方案
您可以轻松地将逗号分隔的列表转换为数组,然后使用 Postgres 的数组函数来测试是否包含:
select id
from the_table
where string_to_array(keyword,',') && array['php','java','html'];
重叠运算符&&
检查从关键字列表创建的数组是否包含右侧数组中的元素。
推荐阅读
- auth0 - 使用 Auth0 进行组织身份管理
- scala - 2FA TOTP 暂存代码或恢复代码是否对订单敏感?
- php - 如何在忽略撇号的同时匹配不完整的单引号?
- single-sign-on - 如何使用keycloak创建既支持承载又支持机密登录的应用程序
- unity3d - Unity 动画在重新定位或调整精灵大小后改变位置
- php - WP下的Ajax请求-脚本简单根本不起作用
- r - 在 R 中使用 read.table() 跳过以“@”开头的行
- c - 为什么 malloc 和 sbrk 从单独的段返回地址?
- android - React Native - 在 Android 上切换选项卡时 ScrollableTabView 很慢
- git - 合并拉取请求后 Git 状态不显示差异