Computer >> Computer tutorials >  >> Programming >> MySQL

MySQL select query with multiple WHERE?


To implement multiple WHERE, use the IN() IN MySQL.

Following is the syntax:

select *from yourTableName where yourColumnName IN(yourValue1,yourValue2,...N);

Let us first create a table -

mysql> create table DemoTable
   (
   Id int,
   Name varchar(100)
   );
Query OK, 0 rows affected (0.61 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable values(10,'John');
Query OK, 1 row affected (0.18 sec)
mysql> insert into DemoTable values(59,'Carol');
Query OK, 1 row affected (0.14 sec)
mysql> insert into DemoTable values(20,'Sam');
Query OK, 1 row affected (0.15 sec)
mysql> insert into DemoTable values(45,'David');
Query OK, 1 row affected (0.73 sec)

Display all records from the table using select statement -

mysql> select *from DemoTable;

Output

+------+-------+
| Id   | Name  |
+------+-------+
| 10   | John  |
| 59   | Carol |
| 20   | Sam   |
| 45   | David |
+------+-------+
4 rows in set (0.00 sec)

Following is the query to implement multiple WHERE -

mysql> select *from DemoTable where Id IN(59,45);

Output

+------+-------+
| Id   | Name  |
+------+-------+
| 59   | Carol |
| 45   | David |
+------+-------+
2 rows in set (0.14 sec)