How to use MySQL LIKE query to search a column value with % in it?

MySQLMySQLi Database

To search a column value with %, the syntax is as follows −

select * from yourTableName
 where yourColumnName LIKE '\%%';

Let us first create a table −

mysql> create table DemoTable1497
   -> (
   -> Name varchar(20)
   -> );
Query OK, 0 rows affected (0.55 sec)

Insert some records in the table using insert −

mysql> insert into DemoTable1497 values('%JohnSmith');
Query OK, 1 row affected (0.14 sec)
mysql> insert into DemoTable1497 values('DavidMiller');
Query OK, 1 row affected (0.18 sec)
mysql> insert into DemoTable1497 values('CarolTaylor%');
Query OK, 1 row affected (0.16 sec)
mysql> insert into DemoTable1497 values('%DavidMiller');
Query OK, 1 row affected (0.12 sec)

Display all records from the table using select −

mysql> select * from DemoTable1497;

This will produce the following output −

+--------------+
| Name         |
+--------------+
| %JohnSmith   |
| DavidMiller  |
| CarolTaylor% |
| %DavidMiller |
+--------------+
4 rows in set (0.00 sec)

Here is the query to use like query to search a column value with % in it −

mysql> select * from DemoTable1497
   -> where Name LIKE '\%%';

This will produce the following output −

+--------------+
| Name         |
+--------------+
| %JohnSmith   |
| %DavidMiller |
+--------------+
2 rows in set (0.00 sec)
raja
Published on 11-Dec-2019 05:41:24
Advertisements