How to search for the exact string value in MySQL?


To search for the exact string value, use the concept of COLLATE. Let us first create a table −

mysql> create table DemoTable1620
    -> (
    -> Subject varchar(20)
    -> );
Query OK, 0 rows affected (0.42 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable1620 values('mysql');
Query OK, 1 row affected (0.13 sec)
mysql> insert into DemoTable1620 values('MySql');
Query OK, 1 row affected (0.11 sec)
mysql> insert into DemoTable1620 values('mYSQL');
Query OK, 1 row affected (0.16 sec)
mysql> insert into DemoTable1620 values('MySQL');
Query OK, 1 row affected (0.26 sec)
mysql> insert into DemoTable1620 values('MYSQL');
Query OK, 1 row affected (0.20 sec)

Display all records from the table using select statement −

mysql> select * from DemoTable1620;

This will produce the following output −

+---------+
| Subject |
+---------+
| mysql   |
| MySql   |
| mYSQL   |
| MySQL   |
| MYSQL   |
+---------+
5 rows in set (0.00 sec)

Following is the query to search for the exact string value in MySQL −

mysql> select * from DemoTable1620 where Subject collate utf8_bin = 'MySQL';

This will produce the following output −

+---------+
| Subject |
+---------+
| MySQL   |
+---------+
1 row in set (0.00 sec)
raja
Published on 08-Nov-2019 11:10:21
Advertisements