- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to search for exact string in MySQL?
You can use binary to search for exact string in MySQL. The syntax is as follows:
SELECT * FROM yourTableName WHERE BINARY yourColumnName = yourStringValue;
To understand the above syntax, let us create a table. The query to create a table is as follows:
mysql> create table ExactSearch -> ( -> Id int NOT NULL AUTO_INCREMENT, -> UserId varchar(10), -> UserName varchar(20), -> PRIMARY KEY(Id) -> ); Query OK, 0 rows affected (0.69 sec)
Insert some records in the table using insert command. The query is as follows:
mysql> insert into ExactSearch(UserId,UserName) values('USER12','John'); Query OK, 1 row affected (0.11 sec) mysql> insert into ExactSearch(UserId,UserName) values('12USER','Carol'); Query OK, 1 row affected (0.20 sec) mysql> insert into ExactSearch(UserId,UserName) values('USER123','Bob'); Query OK, 1 row affected (0.15 sec) mysql> insert into ExactSearch(UserId,UserName) values('USER231','Sam'); Query OK, 1 row affected (0.18 sec)
Display all records from the table using select statement:
mysql> select *from ExactSearch;
The following is the output:
+----+---------+----------+ | Id | UserId | UserName | +----+---------+----------+ | 1 | USER12 | John | | 2 | 12USER | Carol | | 3 | USER123 | Bob | | 4 | USER231 | Sam | +----+---------+----------+ 4 rows in set (0.00 sec)
Here is the query to search for exact string in MySQL. We are searching for string “USER123”:
mysql> select *from ExactSearch where binary UserId = 'USER123';
The following is the output:
+----+---------+----------+ | Id | UserId | UserName | +----+---------+----------+ | 3 | USER123 | Bob | +----+---------+----------+ 1 row in set (0.00 sec)
- Related Articles
- How to search for the exact string value in MySQL?
- How to search a column for an exact string in MySQL?
- MySQL query to search exact word from string?
- How to search a MySQL table for a specific string?
- Quickly search for a string in MySQL database?
- How to search for a string in JavaScript?
- Search for a string within text column in MySQL?
- Find exact string value with COLLATE in MySQL?
- Search for specific characters within a string with MySQL?
- How to search for ^ character in a MySQL table?
- How to search for a pattern in a Java string?
- How to search a string for a pattern in JavaScript?
- How to search for a string in an ArrayList in java?
- How to search for a date in MySQL timestamp field?
- How to search for string or number in a field with MongoDB?

Advertisements