MySQL Multiple Where
MySQL supports the use of multiple where clauses. This is done by using the “AND” clause and the “OR” clause. It doesn’t actually mean that you would use the keyword “WHERE” more than once.
The AND Clause
The “AND” clause is used in cases where you want to match all records that are true for both expressions.
SELECT name, price
FROM products
WHERE id = 5 AND price <= 10;
Combining 3 expressions with ‘AND’:
SELECT name, price
FROM products
WHERE id = 5 AND price <= 10 AND name = 'test';
The OR Clause
The “OR” clause is used in cases where you want to match all records that are true for either expressions but not for both.
SELECT name, price
FROM products
WHERE id = 5 OR price <= 10;
Combining 3 expressions with ‘OR’:
SELECT name, price
FROM products
WHERE id = 5 OR price <= 10 OR name = 'test';
Both AND / OR Together
The AND operator is processed before the OR operator:
SELECT name, price
FROM products
WHERE id = 1008 OR id = 1009 AND price >= 10;
You can use parentheses to change the order of operations:
SELECT name, price
FROM products
WHERE (id = 1008 OR id = 1009) AND price >= 10;