繁体   English   中英

SQL选择一个唯一的字符

[英]SQL selecting for a unique character

在sql中,我想得到所有行,斜杠'/'作为字符只存在一次。

为了exapmle:

[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 '%/%/%'

说明:第一个检查斜杠是否至少出现一次。 第二个检查它没有出现两次。

至少一次,但不到两次,恰好是一次。

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

select * from table1 where path_name not like '/%/%' and path_name not like '/%/';

或者在更换/没有任何东西后计算长度。
WHERE ( LENGTH(col) - LENGTH(REPLACE(col, '/', '')) )=1

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

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'%/%/%'

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM