Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to search for exact string in mysql

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.

like image 246
Wern Ancheta Avatar asked Jul 09 '11 03:07

Wern Ancheta


People also ask

How do I find a specific data in MySQL?

Find data across a MySQL connection by using the text search feature on any number of tables and schemas. From the schema tree, select the tables, schemas, or both to search and then right-click the highlighted items and click Search Data Table from the context menu.

How do I find a specific word in a MySQL database?

MySQL Workbench has this feature too: "Database >> Search Table Data..." Remember to use the % symbol as a wildcard on either side of a string to find any rows containing what you're looking for.

How do I use exact match in SQL?

You should use this query instead for an exact match of the value PC: SELECT * FROM TransDetail TD WHERE TD. ModUserId = 'PC'; When using % in the WHERE clause you are using a wildcard that stands for 0 or more occurrences of characters in that position.


2 Answers

SELECT UserID FROM sys_users WHERE BINARY UserID='NREW'
like image 65
Candide Avatar answered Oct 05 '22 03:10

Candide


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.

like image 42
Michael Mior Avatar answered Oct 05 '22 02:10

Michael Mior