1

在 sql 中,我想获取所有行,其中斜杠“/”作为字符只存在一次。

例如:

[table1]
id | path_name  |
 1 | "/abc/def/"|
 2 | "/abc"     |
 3 | "/a/b/cdfe"|
 4 | "/hello"   |

select * from table1 where path_name=.... ;

所以在这个例子中,我只想有第二行和第四行......

我该如何形成这个声明?

4

5 回答 5

3
where path_name like '%/%' and path_name not like '%/%/%'

或者

where len(path_name) = len(replace(path_name,'/','')) + 1
于 2013-02-28T12:21:45.087 回答
1

要查找正好有一个斜线的表达式:

where path_name like '%/%' and not path_name like '%/%/%'

解释:第一个检查斜线是否至少出现一次。第二个检查它没有出现两次。

至少一次,但少于两次,就是一次。

如果你只想要那些以斜线开头的,你应该将第一个模式更改为'/%'.

于 2013-02-28T12:22:12.870 回答
0

或将 / 替换为空后计算长度。
WHERE ( LENGTH(col) - LENGTH(REPLACE(col, '/', '')) )=1

(感谢如何计算 SQL 列中的字符实例

于 2013-02-28T12:27:26.413 回答
0
select * from table1 where path_name not like '/%/%' and path_name not like '/%/';
于 2013-02-28T12:23:46.727 回答
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'%/%/%'
于 2017-10-17T07:11:44.783 回答