The MySQL WHERE statement is used to filter records based on certain conditions. It is often used in conjunction with SELECT, UPDATE, DELETE, and other MySQL statements. Here are some examples of how the WHERE statement can be used:
- SELECT statement with WHERE clause
The following example selects all records from the “customers” table where the “city” column equals “London”:
SELECT * FROM customers WHERE city = 'London';
- UPDATE statement with WHERE clause
The following example updates the “quantity” column of the “orders” table to 10 where the “product_id” column equals 100:
UPDATE orders SET quantity = 10 WHERE product_id = 100;
- DELETE statement with WHERE clause
The following example deletes all records from the “employees” table where the “age” column is less than 30:
DELETE FROM employees WHERE age < 30;
- IN operator with WHERE clause
The following example selects all records from the “products” table where the “product_id” column is either 1, 2, or 3:
SELECT * FROM products WHERE product_id IN (1, 2, 3);
- LIKE operator with WHERE clause
The following example selects all records from the “customers” table where the “last_name” column starts with “Smi”:
SELECT * FROM customers WHERE last_name LIKE 'Smi%';
These are just a few examples of how the WHERE statement can be used in MySQL. It is a powerful tool that allows you to filter records based on specific conditions, making it easier to manage and manipulate data.