Mysql Combining The And And Or Conditions

MySQL: Combining the AND and OR Conditions

This tutorial explains how to use the AND condition and the OR condition together in a MySQL query with syntax and examples.

Description

The MySQL AND condition and OR condition can be combined in a SELECT, INSERT, UPDATE, or DELETE statement.

When combining these conditions, it is important to use parentheses so that the database knows what order to evaluate each condition. (Just like when you were learning the order of operations in Math class!)

Syntax

The syntax for the AND condition and OR condition together in MySQL is:

WHERE condition1
AND condition2
...
OR condition_n;

Parameters or Arguments

condition1, condition2, ... condition_n

The conditions that are evaluated to determine if the records will be selected.

Note

  • The MySQL AND & OR conditions allow you to test multiple conditions.
  • Don't forget the order of operation parentheses!

Example - With SELECT Statement

Let's look at an example that combines the AND and OR conditions in a SELECT statement.

For example:

SELECT *
FROM customers
WHERE (state = 'California' AND last_name = 'Johnson')
OR (customer_id > 4500);

This AND & OR example would return all suppliers that reside in the state of California whose last_name is Johnson and all suppliers whose customer_id is greater than 4500. The parentheses determine the order that the AND and OR conditions are evaluated. Just like you learned in the order of operations in Math class!

The next example takes a look at a more complex statement.

For example:

SELECT customer_id, last_name, first_name
FROM customers
WHERE (last_name = 'Johnson')
OR (last_name = 'Mark' AND state = 'California')
OR (last_name = 'Smith' AND status = 'Active' AND state = 'Florida');

This AND & OR example would return all customer_id, last_name, and first_name values from the customers table whose the last_name is Johnson OR whose last_name is Mark and the state is California OR whose last_name is Smith, the status is Active and the state is Florida.

Example - With INSERT Statement

This next AND & OR example demonstrates how the AND condition and OR condition can be combined in the INSERT statement.

For example:

INSERT INTO suppliers
(supplier_id, supplier_name)
SELECT customer_id, customer_name
FROM customers
WHERE (customer_name = 'Apple' OR customer_name = 'Samsung')
AND customer_id >= 100;

This MySQL AND and OR example would insert into the suppliers table, all customer_id and customer_name records from the customers table whose customer_name is either Apple or Samsung and where the customer_id is greater than or equal to 100.

Example - With UPDATE Statement

This AND & OR example shows how the AND and OR conditions can be used in the UPDATE statement.

For example:

UPDATE contacts
SET last_name = 'Johnson'
WHERE last_name = 'Mark'
AND (state = 'Florida' OR state = 'California');

This MySQL AND & OR condition example would update all last_name values in the contacts table to Johnson where the last_name was Mark and resides in either the state of Florida or 'California'.

Example - With DELETE Statement

Finally, this last AND & OR example demonstrates how the AND and OR conditions can be used in the DELETE statement.

For example:

DELETE FROM contacts
WHERE state = 'California'
AND (last_name = 'Smith' OR last_name = 'Mark');

This MySQL AND and OR condition example would delete all records from the contacts table whose state is California and last_name was either Smith or Mark.