This tutorial explains how to use the MySQL OR condition with syntax and examples.
The MySQL OR Condition is used to test two or more conditions where records are returned when any one of the conditions are met. It can be used in a SELECT, INSERT, UPDATE, or DELETE statement.
The syntax for the OR Condition in MySQL is:
Any of the conditions that must be met for the records to be selected.
The first MySQL OR condition example that we'll take a look at involves a SELECT statement with 2 conditions:
This MySQL OR condition example would return all customers that reside in either the state of California or have a contact_id less than 1000. Because the * is used in the SELECT statement, all fields from the contacts table would appear in the result set.
The next MySQL OR example looks at a SELECT statement with 3 conditions. If any of these conditions is met, the record will be included in the result set.
This MySQL OR condition example would return all supplier_id and supplier_name values where the supplier_name is either Microsoft, the state is Florida, or offices is greater than 10.
The MySQL OR condition can be used in the INSERT statement.
For example:
This MySQL OR example would insert into the suppliers table, all customer_id and customer_name records from the customers table that reside in the state of Florida or California.
The MySQL OR condition can be used in the UPDATE statement.
For example:
This MySQL OR condition example would update all supplier_name values in the suppliers table to Apple where the supplier_name was RIM or its available_products was greater than 25.
The MySQL OR condition can be used in the DELETE statement.
For example:
This MySQL OR condition example would delete all customers from the customers table whose last_name was Johnson or first_name was Joe.