简体   繁体   English

类似条件下的 Sql 模式

[英]Sql pattern in like condition

Simple issue :简单的问题:

I have fields (path) like :我有如下字段(路径):

1/2/43
1/2/43/45
1/2/43/45/46

I want to be able to get the path containing 43/XX我希望能够获得包含 43/XX 的路径

Meanings here the only valid one would be此处唯一有效的含义是

1/2/43/45

This seems not to be working这似乎不起作用

... WHERE path LIKE '%43/[0-9][0-9]%'

Only SQL Server supports using (a small subset of) regular expressions with LIKE .只有 SQL Server 支持在LIKE使用(一小部分)正则表达式。 In MySQL, you would use RLIKE , or REGEXP (both are synonyms).在 MySQL 中,您将使用RLIKEREGEXP (两者都是同义词)。 Your condition would translate as:您的情况将转换为:

WHERE path RLIKE '43/[0-9][0-9]'

This can be shortened with a quantifier:这可以用量词缩短:

WHERE path RLIKE '43/[0-9]{2}'

You might want to be a little more specific by ensuring that the character that precedes 43 is either a slash or the beginning of the string:您可能希望通过确保43之前的字符是斜杠或字符串的开头来更具体一点:

WHERE path RLIKE '(^|/)43/[0-9]{2}'

The same constraint can be applied to the right side of the string:相同的约束可以应用于字符串的右侧:

WHERE path RLIKE '(^|/)43/[0-9]{2}(/|$)'

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

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