Syntax error SELECT where row value contains string in MySQL?

SELECT where row value contains string in MySQL?



To select the row value containing string in MySQL, use the following syntax.

SELECT *FROM yourTableName where yourColumnName like ‘%yourPattern%’;

To understand the above syntax, let us first create a table. The query to create a table is as follows.

mysql> create table PatternDemo
-> (
-> Id int,
-> Name varchar(100),
-> Age int
-> );
Query OK, 0 rows affected (0.97 sec)

Insert records in the table using insert command. The query is as follows.

mysql> insert into PatternDemo values(1,'James',23);
Query OK, 1 row affected (0.11 sec)

mysql> insert into PatternDemo values(2,'Joseph',21);
Query OK, 1 row affected (0.18 sec)

mysql> insert into PatternDemo values(3,'Robert',20);
Query OK, 1 row affected (0.11 sec)

mysql> insert into PatternDemo values(4,'John',26);
Query OK, 1 row affected (0.10 sec)

mysql> insert into PatternDemo values(5,'Richard',24);
Query OK, 1 row affected (0.17 sec)

Now you can display all records from the table using select statement. The query is as follows.

mysql> select *from PatternDemo;

The following is the output.

+------+---------+------+
| Id   | Name    | Age  |
+------+---------+------+
| 1    | James   | 23   |
| 2    | Joseph  | 21   |
| 3    | Robert  | 20   |
| 4    | John    | 26   |
| 5    | Richard | 24   |
+------+---------+------+
5 rows in set (0.00 sec)

Here is the query that will give all names with the characters “Jo”. The query is as follows.

mysql> select *from PatternDemo where Name like '%Jo%';

The following is the output.

+------+--------+------+
| Id   | Name   | Age  |
+------+--------+------+
| 2    | Joseph | 21   |
| 4    | John   | 26   |
+------+--------+------+
2 rows in set (0.00 sec)
Updated on: 2023-10-05T01:06:33+05:30

40K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements