MySQL query to conduct a basic search for a specific last name in a column


You can use LIKE operator to conduct a basic search for last name. Let us first create a table: −

mysql> create table DemoTable
   -> (
   -> CustomerName varchar(100),
   -> CustomerAge int
   -> );
Query OK, 0 rows affected (0.77 sec)

Insert some records in the table using insert command −

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

mysql> insert into DemoTable values('David Miller',24);
Query OK, 1 row affected (0.18 sec)

mysql> insert into DemoTable values('Bob Doe',27);
Query OK, 1 row affected (0.18 sec)

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

Display all records from the table using select statement −

mysql> select *from DemoTable;

This will produce the following output −

+--------------+-------------+
| CustomerName | CustomerAge |
+--------------+-------------+
| John Doe     |          34 |
| David Miller |          24 |
| Bob Doe      |          27 |
| Carol Taylor |          29 |
+--------------+-------------+
4 rows in set (0.00 sec)

Following is the query to search for last name from a column −

mysql> select *from DemoTable where CustomerName LIKE '%Doe%';

This will produce the following output −

+--------------+-------------+
| CustomerName | CustomerAge |
+--------------+-------------+
| John Doe     |          34 |
| Bob Doe      |          27 |
+--------------+-------------+
2 rows in set (0.00 sec)
raja
Published on 12-Jul-2019 11:28:44
Advertisements