在sql中,我想得到所有行,斜杠'/'作为字符只存在一次。
exapmle:
[table1]
id | path_name |
1 | "/abc/def/"|
2 | "/abc" |
3 | "/a/b/cdfe"|
4 | "/hello" |
select * from table1 where path_name=.... ;
所以在这个例子中,我想只有第二和第四行......
我该如何形成这个陈述?
答案 0 :(得分:3)
where path_name like '%/%' and path_name not like '%/%/%'
或
where len(path_name) = len(replace(path_name,'/','')) + 1
答案 1 :(得分:1)
要查找只有一个斜杠的表达式:
where path_name like '%/%' and not path_name like '%/%/%'
说明:第一个检查斜杠是否至少出现一次。第二个检查它没有出现两次。
至少一次,但不到两次,恰好是一次。
如果您只想要以斜线开头的那些,则应将第一个模式更改为'/%'
。
答案 2 :(得分:0)
select * from table1 where path_name not like '/%/%' and path_name not like '/%/';
答案 3 :(得分:0)
或更换/没有任何东西后计算长度。
WHERE ( LENGTH(col) - LENGTH(REPLACE(col, '/', '')) )=1
答案 4 :(得分:0)
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'%/%/%'