Syntax error How to use MySQL LIKE clause to fetch multiple values beginning with “Joh”

How to use MySQL LIKE clause to fetch multiple values beginning with “Joh”



Let us first create a table −

mysql> create table DemoTable
(
   Name varchar(40)
);
Query OK, 0 rows affected (0.55 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable values('John');
Query OK, 1 row affected (0.10 sec)
mysql> insert into DemoTable values('Sam');
Query OK, 1 row affected (0.13 sec)
mysql> insert into DemoTable values('Mike');
Query OK, 1 row affected (0.11 sec)
mysql> insert into DemoTable values('Ethan');
Query OK, 1 row affected (0.10 sec)
mysql> insert into DemoTable values('Johnson');
Query OK, 1 row affected (0.16 sec)
mysql> insert into DemoTable values('Bob');
Query OK, 1 row affected (0.12 sec)

Display all records from the table using select statement −

mysql> select *from DemoTable;

This will produce the following output −

+---------+
| Name    |
+---------+
| John    |
| Sam     |
| Mike    |
| Ethan   |
| Johnson |
| Bob     |
+---------+
6 rows in set (0.00 sec)

Following is the query to fetch multiple values beginning with “Joh” −

mysql> select *from(
   select Name as myValue from DemoTable union
   select Name from DemoTable
)tbl
where myValue like 'Joh%';

This will produce the following output −

+---------+
| myValue |
+---------+
| John    |
| Johnson |
+---------+
2 rows in set (0.00 sec)
Updated on: 2019-10-07T11:47:15+05:30

204 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements