Can I use two where clauses like “SELECT * FROM table WHERE condition1 and condition2” in MySQL?


Yes, you need to use AND or OR operator. The syntax is as follows −

select *from yourTableName where yourColumnName1=yourValue AND
yourColumnName=yourValue';

For AND condition, both conditions must be true otherwise you will get an empty set.

To understand the above syntax, let us create a table. The query to create a table is as follows −

mysql> create table WhereDemo
   -> (
   -> Id int,
   -> Name varchar(20)
   -> );
Query OK, 0 rows affected (0.56 sec)

Now you can insert some records in the table using insert command. The query is as follows −

mysql> insert into WhereDemo values(101,'Maxwell');
Query OK, 1 row affected (0.14 sec)
mysql> insert into WhereDemo values(110,'David');
Query OK, 1 row affected (0.21 sec)
mysql> insert into WhereDemo values(1000,'Carol');
Query OK, 1 row affected (0.18 sec)
mysql> insert into WhereDemo values(1100,'Bob');
Query OK, 1 row affected (0.47 sec)
mysql> insert into WhereDemo values(115,'Sam');
Query OK, 1 row affected (0.23 sec)

Display all records from the table using select statement. The query is as follows −

mysql> select *from WhereDemo;

The following is the output −

+------+---------+
| Id   | Name    |
+------+---------+
|  101 | Maxwell |
|  110 | David   |
| 1000 | Carol   |
| 1100 | Bob     |
|  115 | Sam     |
+------+---------+
5 rows in set (0.00 sec)

Here is the query to select all records from the table with more than one condition −

mysql> select *from WhereDemo where Id=1100 AND Name='Bob';

The following is the output −

+------+------+
| Id   | Name |
+------+------+
| 1100 | Bob  |
+------+------+
1 row in set (0.00 sec)

Samual Sam
Samual Sam

Learning faster. Every day.

Updated on: 30-Jul-2019

228 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements