sql - 如何在字符序列正则表达式SQL中找到相同的两位数字
问题描述
我想打印出在 SQL 中至少重复两个数字的字符串
EX
11-22-33
11-22-44
22-22-33
55-22-33
11-66-33
11-88-33
33-88-33
77-77-22
输出 :
22-22-33
77-77-22
33-88-33
但我不知道如何编写一个对我有帮助的正则表达式
解决方案
对于这种固定格式NN-NN-NN
,您可以只使用字符串函数并测试三种可能的组合:
select *
from mytable
where substr(val, 1, 2) = substr(val, 3, 2)
or substr(val, 1, 2) = substr(val, 5, 2)
or substr(val, 3, 2) = substr(val, 5, 2)
我们可以花点心思,使用横向连接代替重复or
条件。如果您有 3 个以上的部分,这将更好地扩展(组合的数量迅速增加,这使得or
解决方案不太方便):
select t.*
from mytable t
cross apply (
select count(distinct part) cnt_distinct_part
from (
select substr(t.val, 1, 2) part
union all select substr(t.val, 3, 2)
union all select substr(t.val, 5, 2)
) x
) x
where x.cnt_distinct_part < 3
推荐阅读
- ios - URLSession的正确用法是什么,创建新的或重复使用相同的
- macos - 自定义 iCal 日历未在 Mac 上创建通知
- phpstorm - 如何在 PhpStorm 的工具下为“启动 SSH 会话...”创建快捷方式
- sql - 在 oracle 中创建特定的值序列
- .net - 我可以卸载 .NET Framework 副本吗?
- multithreading - 为什么 View 中的请求会导致树侦听器在非 gui 模式下显示“没有要显示的数据”
- amazon-redshift - 创建具有对给定架构(包括删除表)的访问权限的 aws redshift 用户
- jquery - 如何获取元素的父母、祖父母和曾祖父母?
- unity3d - 画布缩放器问题
- sitecore - Sitecore Commerce 商务工具无法加载