How to search a MySQL table for a specific string?


Use equal to operator for an exact match −

select *from yourTableName where yourColumnName=yourValue;

Let us first create a table −

mysql> create table DemoTable
   -> (
   -> FirstName varchar(100),
   -> LastName varchar(100)
   -> );
Query OK, 0 rows affected (0.70 sec

Insert some records in the table using insert command −

mysql> insert into DemoTable values('John','Smith');
Query OK, 1 row affected (0.14 sec)

mysql> insert into DemoTable values('John','Doe');
Query OK, 1 row affected (0.19 sec)

mysql> insert into DemoTable values('Chris','Brown');
Query OK, 1 row affected (0.13 sec)

mysql> insert into DemoTable values('Carol','Taylor');
Query OK, 1 row affected (0.29 sec)

Display all records from the table using select statement −

mysql> select *from DemoTable;

This will produce the following output −

+-----------+----------+
| FirstName | LastName |
+-----------+----------+
| John      | Smith    |
| John      | Doe      |
| Chris     | Brown    |
| Carol     | Taylor   |
+-----------+----------+
4 rows in set (0.00 sec)

Following is the query to search a MySQL table for a specific string −

mysql> select *from DemoTable where LastName='Doe';

This will produce the following output −

+-----------+----------+
| FirstName | LastName |
+-----------+----------+
| John      | Doe      |
+-----------+----------+
1 row in set (0.00 sec)
raja
Published on 12-Jul-2019 11:42:17
Advertisements