簡體   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