Working with MySQL WHERE.. OR query with multiple OR usage. Is there an alternative?


Yes, an alternative for MySQL “WHERE.. OR” is using REGEXP.

Let us first create a table −

mysql> create table DemoTable684(EmployeeInformation text);
Query OK, 0 rows affected (0.68 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable684 values('John 21 Google');
Query OK, 1 row affected (0.15 sec)
mysql> insert into DemoTable684 values('Carol 23 Amazon');
Query OK, 1 row affected (0.16 sec)
mysql> insert into DemoTable684 values('Carol 26 Flipkart');
Query OK, 1 row affected (0.12 sec)
mysql> insert into DemoTable684 values('David 29 Microsoft');
Query OK, 1 row affected (0.18 sec)

Display all records from the table using select statement −

mysql> select *from DemoTable684;

This will produce the following output −

+---------------------+
| EmployeeInformation |
+---------------------+
| John 21 Google      |
| Carol 23 Amazon     |
| Carol 26 Flipkart   |
| David 29 Microsoft  |
+---------------------+
4 rows in set (0.00 sec)

Following is the query for implementing MySQL where...OR using REGEXP −

mysql> select *from DemoTable684 where EmployeeInformation REGEXP '(David|29|Microsoft)';

This will produce the following output −

+---------------------+
| EmployeeInformation |
+---------------------+
| David 29 Microsoft  |
+---------------------+
1 row in set (0.00 sec)

Updated on: 26-Aug-2019

113 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements