MySQL LIKE query with dynamic array?

To implement LIKE query with dynamic array, the syntax is as follows −

Example

select *from yourTableName
   where yourColumnName2 like "%yourValue%"
   order by yourColumnName1 asc
   limit yourLimitValue;

Let us create a table −

Example

mysql> create table demo74
   -> (
   -> user_id int not null auto_increment primary key,
   -> user_names varchar(250)
   -> )
   -> ;
Query OK, 0 rows affected (0.67

Insert some records into the table with the help of insert command −

Example

mysql> insert into demo74(user_names) values("John Smith1,John Smith2,John Smith3");
Query OK, 1 row affected (0.18

mysql> insert into demo74(user_names) values("John Smith1");
Query OK, 1 row affected (0.15

mysql> insert into demo74(user_names) values("David Smith1");
Query OK, 1 row affected (0.12

mysql> insert into demo74(user_names) values("John Smith1,John Smith2,John Smith3,John Smith4");
Query OK, 1 row affected (0.10

Display records from the table using select statement −

Example

mysql> select *from demo74;

This will produce the following output −

Output

<p>+---------+-------------------------------------------------+</p><p>| user_id | user_names                                      |</p><p>+---------+-------------------------------------------------+</p><p>|       1 | John Smith1,John Smith2,John Smith3             |</p><p>|       2 | John Smith1                                     |</p><p>|       3 | David Smith1                                    |</p><p>|       4 | John Smith1,John Smith2,John Smith3,John Smith4 |</p><p>+---------+-------------------------------------------------+</p>

Following is the MySQL LIKE query with dynamic array −

Example

mysql> select *from demo74
-> where user_names like "%John Smith1%"
-> order by user_id asc
-> limit 100;

This will produce the following output −

Output

<p>+---------+-------------------------------------------------+</p><p>| user_id | user_names                                      |</p><p>+---------+-------------------------------------------------+</p><p>|       1 | John Smith1,John Smith2,John Smith3             |</p><p>|       2 | John Smith1                                     |</p><p>|       4 | John Smith1,John Smith2,John Smith3,John Smith4 |</p><p>+---------+-------------------------------------------------+</p><p>3 rows in set (0.00 sec)</p>
Updated on: 2020-12-11T05:23:57+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements