在 sql 中,我想获取所有行,其中斜杠“/”作为字符只存在一次。
例如:
[table1]
id | path_name |
1 | "/abc/def/"|
2 | "/abc" |
3 | "/a/b/cdfe"|
4 | "/hello" |
select * from table1 where path_name=.... ;
所以在这个例子中,我只想有第二行和第四行......
我该如何形成这个声明?
where path_name like '%/%' and path_name not like '%/%/%'
或者
where len(path_name) = len(replace(path_name,'/','')) + 1
要查找正好有一个斜线的表达式:
where path_name like '%/%' and not path_name like '%/%/%'
解释:第一个检查斜线是否至少出现一次。第二个检查它没有出现两次。
至少一次,但少于两次,就是一次。
如果你只想要那些以斜线开头的,你应该将第一个模式更改为'/%'
.
或将 / 替换为空后计算长度。
WHERE ( LENGTH(col) - LENGTH(REPLACE(col, '/', '')) )=1
(感谢如何计算 SQL 列中的字符实例)
select * from table1 where path_name not like '/%/%' and path_name not like '/%/';
1.select * from Table1 where len(path_name)-len(replace(path_name,'/',''))= 1
2.select * from table1 where len(path_name)= len(replace(path_name,'/',''))+1
3.select * from table1 where path_name like '%/%' and path_name not like'%/%/%'