sql - 如何在 SQL 中查询多个具有位置的子字符串?
问题描述
我有以下查询:
select position( '/s/' in col)
from table
这行得通。
现在,我希望能够找到/s/
or /d/
or的位置/c/
我试过:
select position( '[/s/][/c/][/d/]' in col)
from table
但它返回一个错误的值。
例子:
select position( '/s/' in '/s/')
返回 1。
select position( '[/s/][/d/][/c/]' in '/s/')
返回 0
我该如何解决这个问题?
编辑:
它只能包含
/s/
or/d/
或之一/c/
。该字符串将不包含/s/d/
等...匹配的子字符串不能再出现一次。该字符串将不包含
'/s/..../s/'
简而言之 - 我正在寻找第一次出现的/s/ or /d/ or /c/
不要担心边缘情况。
解决方案
这可能会奏效:
SELECT MAX(patlist.pattern) FROM (VALUES (POSITION('/s/' IN col)), (POSITION('/d/' IN col)), (POSITION('/c/' IN col))) AS patlist(pattern)
包装在查询中:
SELECT (SELECT MAX(patlist.pattern) FROM (VALUES (POSITION('/s/' IN col)), (POSITION('/d/' IN col)), (POSITION('/c/' IN col))) AS patlist(pattern))
FROM MyTable
免责声明:在没有 prestodb 实例的情况下,我只能在备用数据库引擎上对其进行测试。它可能适用于 prestodb,也可能不适用。
推荐阅读
- java - 我没有在 Java 中获得回文程序的输出
- svg - 结合几个SVG
- java - 如何为用户队列设计数据库
- asp.net - ID 为#### 的进程未运行。输出显示:“程序‘[4560] iisexpress.exe’已退出,代码为 0 (0x0)。”
- python - 我如何在整个数据集上应用数据增强
- python - 如何使用 joblib.Parallel() 来并行化一个带有一些“继续”的循环?
- python - AttributeError: '_tkinter.tkapp' 对象没有属性 'destoy
- arrays - 为什么我会从结果中重复的 WP_query 调用中获得第一个和最后一个帖子?
- css - 弹性项目在默认设置下不收缩
- vue.js - 如何按类别过滤数据数组?