The WHERE clause in MySQL is used to filter records that meet a specific condition. It is commonly used with SELECT queries in PHP-MySQL applications to retrieve only the required data.
// Basic SELECT query with WHERE condition
SELECT column1, column2
FROM table_name
WHERE condition;
// Fetch users older than 30
SELECT * FROM users
WHERE age > 30;
Only users whose age value is greater than 30 will be returned from the database.
// Multiple conditions using AND
SELECT * FROM users
WHERE age > 30 AND city = 'New York';
// Either condition using OR
SELECT * FROM users
WHERE age > 30 OR city = 'New York';
// Pattern matching using LIKE
SELECT * FROM users
WHERE username LIKE 'J%';
// Filter using multiple values
SELECT * FROM users
WHERE city IN ('New York','Los Angeles');
Imagine a PHP login page that only fetches users with a matching email. The WHERE clause makes this possible efficiently.