MySQL LIKE 语句在除字符串开头之外的任何位置查找子字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1918224/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
MySQL LIKE statement to find a substring at any location except the start of the string
提问by Anthony
I have a SQL query SELECT * FROM table WHERE column LIKE '%pdd%'
.
我有一个 SQL 查询SELECT * FROM table WHERE column LIKE '%pdd%'
。
The problem is I need to get all results excluding those which start with "pdd", by which I mean find everything where "pdd" is not at the beginning. How could it be done?
问题是我需要获得所有结果,不包括以“pdd”开头的结果,我的意思是找到“pdd”不在开头的所有内容。怎么可能呢?
I do need to match "pdd" when it is not at the beginning of the column.
当“pdd”不在列的开头时,我确实需要匹配它。
回答by Bill Karwin
I assume you mean all rows that match "pdd" except those where "pdd" is at the beginning.
我假设你的意思是所有匹配“pdd”的行,除了那些“pdd”在开头的行。
SELECT * FROM table WHERE column LIKE '_%pdd%'.
The "_
" wildcard in LIKE
predicates means "one of any character," equivalent to ".
" in regular expressions.
谓词中的“ _
”通配符LIKE
表示“任何字符之一”,相当于.
正则表达式中的“ ”。
回答by quosoo
If I understand your requirement correctly what you need is:
如果我正确理解您的要求,您需要的是:
SELECT * FROM table WHERE column LIKE '%pdd_%' and column NOT LIKE 'pdd_%'
回答by Marc
SELECT * FROM table WHERE column LIKE '%pdd%' AND column NOT LIKE 'pdd%'
You can optimise the query depending on how frequent these occurrences are in your table.
您可以根据这些出现在您的表中的频率来优化查询。
回答by tpow
Try:
尝试:
SELECT * FROM table WHERE column NOT LIKE 'pdd%'