how to search for exact string in mysql

自古美人都是妖i 提交于 2020-01-09 09:24:12

问题


I'm trying to search for an exact match of a string in mysql. The string is 'nrew'. But when I do the queries below, I still get a result:

SELECT UserID FROM sys_users WHERE UserID='NREW'
SELECT UserID FROM sys_users WHERE UserID='NrEw'

Please help.


回答1:


SELECT UserID FROM sys_users WHERE BINARY UserID='NREW'



回答2:


The default collation which MySQL uses to make comparisons is case insensitive. You need to specify a case sensitive collation or binary. You can either do this when creating the column, or in the query.

For example:

SELECT UserID FROM sys_users WHERE UserID='NREW' COLLATE latin1_bin

The proper collation depends on your character set. For latin1, the default, you can use latin1_bin. For utf8, utf8_bin.




回答3:


You can use keyword Binary,

SELECT UserID FROM sys_users WHERE BINARY UserID='nrew'

refer to here




回答4:


One method is to use LIKE BINARY instead of =:

SELECT UserID FROM sys_users WHERE UserID LIKE BINARY 'nrew';



回答5:


Give this is try: http://www.devx.com/tips/Tip/13043

Select UserID from sys_users where convert(varbinary, UserID) = convert(varbinary, 'NREW') 

Or try COLLATE

http://aspadvice.com/blogs/ssmith/archive/2007/09/30/Case-Sensitive-or-Insensitive-SQL-Query.aspx



来源:https://stackoverflow.com/questions/6632598/how-to-search-for-exact-string-in-mysql

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!