MYSQL 搜索字符串是否包含特殊字符?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33538420/
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 search if a string contains special characters?
提问by Al Amin Chayan
I need to search table field contains special characters. I found a solution given heresomething like:
我需要搜索包含特殊字符的表字段。我在这里找到了一个解决方案,例如:
SELECT *
FROM `tableName`
WHERE `columnName` LIKE "%#%"
OR `columnName` LIKE "%$%"
OR (etc.)
But this solution is too broad. I need to mention all the special characters. But I want something which search something like:
但是这个解决方案太宽泛了。我需要提及所有特殊字符。但我想要一些搜索类似的东西:
SELECT *
FROM `tableName`
WHERE `columnName` LIKE '%[^a-zA-Z0-9]%'
That is search column which contains not only a-z, A-Z and 0-9 but some other characters also. Is it possible with MYSQL
那是搜索列,它不仅包含 az、AZ 和 0-9,还包含一些其他字符。有没有可能MYSQL
回答by Avinash Raj
Use regexp
用 regexp
SELECT *
FROM `tableName`
WHERE `columnName` REGEXP '[^a-zA-Z0-9]'
This would select all the rows where the particular column contain atleast one non-alphanumeric character.
这将选择特定列包含至少一个非字母数字字符的所有行。
or
或者
REGEXP '[^[:alnum:]]'