Can we fetch multiple values with MySQL WHERE Clause?


Yes, we can fetch, but use MySQL OR for conditions. Let us first create a −

mysql> create table DemoTable1421
   -> (
   -> EmployeeId int NOT NULL AUTO_INCREMENT PRIMARY KEY,
   -> EmployeeName varchar(20),
   -> EmployeeSalary int
   -> );
Query OK, 0 rows affected (0.82 sec)

Insert some records in the table using insert −

mysql> insert into DemoTable1421(EmployeeName,EmployeeSalary) values('Chris',10000);
Query OK, 1 row affected (0.17 sec)
mysql> insert into DemoTable1421(EmployeeName,EmployeeSalary) values('Bob',15000);
Query OK, 1 row affected (0.17 sec)
mysql> insert into DemoTable1421(EmployeeName,EmployeeSalary) values('David',8000);
Query OK, 1 row affected (0.09 sec)
mysql> insert into DemoTable1421(EmployeeName,EmployeeSalary) values('Carol',8500);
Query OK, 1 row affected (0.39 sec)
mysql> insert into DemoTable1421(EmployeeName,EmployeeSalary) values('Mike',14500);
Query OK, 1 row affected (0.16 sec)

Display all records from the table using select −

mysql> select * from DemoTable1421;

This will produce the following output −

+------------+--------------+----------------+
| EmployeeId | EmployeeName | EmployeeSalary |
+------------+--------------+----------------+
|          1 | Chris       |           10000 |
|          2 | Bob         |           15000 |
|          3 | David       |            8000 |
|          4 | Carol       |            8500 |
|          5 | Mike        |           14500 |
+------------+--------------+----------------+
5 rows in set (0.00 sec)

Here is the query to fetch multiple values with OR −

mysql> select * from DemoTable1421 where EmployeeId=1 OR EmployeeName='David' OR EmployeeSalary=14500;

This will produce the following output −

+------------+--------------+----------------+
| EmployeeId | EmployeeName | EmployeeSalary |
+------------+--------------+----------------+
|          1 | Chris        |          10000 |
|          3 | David        |           8000 |
|          5 | Mike         |          14500 |
+------------+--------------+----------------+
3 rows in set (0.00 sec)

Updated on: 12-Nov-2019

147 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements