【发布时间】:2019-09-18 14:22:45
【问题描述】:
我有一列用“:”冒号分隔不同的值。我想匹配列中的确切字符串。 让我们假设表是临时的,列名是 sample_col。值的顺序也可以改变。
sr_no|sample_col
1 | 'Sample-ABC'
2 | 'Sample-ABC-123'
3 | 'Sample-ABC-123:XYZ'
4 | 'Sample-ABC-123:Sample-ABC'
5 | 'XYZ:Sample-ABC'
所以
1) 需要查询来搜索值为“Sample-ABC”的记录,然后是 3 条记录(即 sr_no 1,4,5) 应该通过查询来实现
2) 需要查询来搜索值为“Sample-ABC-123”的记录,然后是 3 条记录(即 sr_no 2,3,4) 应该获取。
3) 对于那些具有“Sample-ABC-123”值但没有“Sample-ABC”的记录需要解决方案,然后应该获取 2 条记录(即 sr_no 2,3)。
我尝试使用 regexp_like、instr、like 等选项获取输出,但失败了。谁能帮我整理一下。
下面是我尝试获取具有“Sample-ABC”记录的 sql 查询:
select * from temp
where instr(sample_col,'Sample-ABC') > 1
and sample_col not like '%Sample-ABC-123%'; --this exclude 'Sample-ABC-123:Sample-ABC' record from
select * from temp
where sample_col not like '%Sample-ABC%'; --this will include 'Sample-ABC-123' records as well.
select * from temp
where regexp_like(sample_col,'Sample-ABC'); --this will include 'Sample-ABC-123' records as well.
我需要查询,它将提供所有的输出
【问题讨论】: