MySQL database field type for search query?


Following is the syntax −

select *from yourTableName where REGEXP_INSTR(yourColumnName,yourSearchValue);

To understand the above syntax, let us first create a table −

mysql> create table demo64
−> (
−> id int not null auto_increment primary key,
−> name varchar(40)
−> );
Query OK, 0 rows affected (3.06 sec)

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

mysql> insert into demo64(name) values('John Smith');
Query OK, 1 row affected (0.21 sec)

mysql> insert into demo64(name) values('John Doe');
Query OK, 1 row affected (0.15 sec)

mysql> insert into demo64(name) values('Chris Brown');
Query OK, 1 row affected (0.08 sec)

mysql> insert into demo64(name) values('David Miller');
Query OK, 1 row affected (0.20 sec)

mysql> insert into demo64(name) values('Carol Taylor');
Query OK, 1 row affected (0.10 sec)

Display records from the table using select statement −

mysql> select *from demo64;

This will produce the following output −

+----+--------------+
| id | name         |
+----+--------------+
|  1 | John Smith   |
|  2 | John Doe     |
|  3 | Chris Brown  |
|  4 | David Miller |
|  5 | Carol Taylor |
+----+--------------+
5 rows in set (0.00 sec)

Following is the query for field type search query −

mysql> select *from demo64 where REGEXP_INSTR(name,'John');

This will produce the following output −

+----+------------+
| id | name       |
+----+------------+
|  1 | John Smith |
|  2 | John Doe   |
+----+------------+
2 rows in set (0.00 sec)

Updated on: 20-Nov-2020

115 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements