This tutorial explains how to use the MySQL AND condition with syntax and examples.
The MySQL AND Condition (also called the AND Operator) is used to test two or more conditions in a SELECT, INSERT, UPDATE, or DELETE statement.
The syntax for the AND Condition in MySQL is:
All of the conditions that must be met for the records to be selected.
Let's look at some examples that show how to use the AND condition in MySQL.
The first MySQL AND condition query involves a SELECT statement with 2 conditions.
For example:
This MySQL AND example would return all contacts that reside in the state of California and have a customer_id greater than 3000. Because the * is used in the SELECT statement, all fields from the contacts table would appear in the result set.
Our next MySQL AND example shows how the AND condition can be used to join multiple tables in a SELECT statement.
For example:
Though the above SQL works just fine, you would more traditionally write this SQL as follows using a proper INNER JOIN.
For example:
This MySQL AND condition example would return all rows where the supplier_name is Dell. And the suppliers and orders tables are joined on supplier_id. You will notice that all of the fields are prefixed with the table names (ie: orders.order_id). This is required to eliminate any ambiguity as to which field is being referenced; as the same field name can exist in both the suppliers and the orders tables.
In this case, the result set would only display the order_id and supplier_name fields (as listed in the first part of the SELECT statement.).
This next MySQL AND example demonstrates how the AND condition can be used in the INSERT statement.
For example:
This MySQL AND condition example would insert into the suppliers table, all customer_id and customer_name records from the customers table whose customer_name is Oracle and have a customer_id less than 2500.
This MySQL AND condition example shows how the AND condition can be used in the UPDATE statement.
For example:
This MySQL AND condition example would update all supplier_name values in the suppliers table to Cisco where the supplier_name was Sun Microsystems and had 10 offices.
Finally, this last MySQL AND example demonstrates how the AND condition can be used in the DELETE statement.
For example:
This MySQL AND condition example would delete all records from the suppliers table whose supplier_name was Gateway and product was computers.
Learn more about joining tables in MySQL.