Fetch a specific column value (name) in MySQL

MySQLMySQLi Database

To fetch a specific column value, use LIKE clause. Let us first create a table −

mysql> create table DemoTable1809
     (
     Name varchar(20)
     );
Query OK, 0 rows affected (0.00 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable1809 values('John');
Query OK, 1 row affected (0.00 sec)
mysql> insert into DemoTable1809 values('David');
Query OK, 1 row affected (0.00 sec)
mysql> insert into DemoTable1809 values('Mike');
Query OK, 1 row affected (0.00 sec)
mysql> insert into DemoTable1809 values('Johnson');
Query OK, 1 row affected (0.00 sec)

Display all records from the table using select statement −

mysql> select * from DemoTable1809;

This will produce the following output −

+---------+
| Name    |
+---------+
| John    |
| David   |
| Mike    |
| Johnson |
+---------+
4 rows in set (0.00 sec)

Here is the query to fetch a specific value

mysql> select * from DemoTable1809 where Name LIKE 'Johnson%';

This will produce the following output −

+---------+
| Name    |
+---------+
| Johnson |
+---------+
1 row in set (0.00 sec)
raja
Published on 24-Dec-2019 06:10:32
Advertisements