📜  MySQL WHERE - SQL (1)

📅  最后修改于: 2023-12-03 14:44:27.835000             🧑  作者: Mango

MySQL WHERE - SQL

The WHERE clause in MySQL is used to filter records while retrieving data from a table. It allows you to specify conditions based on which the database selects the desired rows from a table.

Syntax

The basic syntax of the WHERE clause is as follows:

SELECT column1, column2, ...
FROM table_name
WHERE condition;

Here, column1, column2, ... refers to the columns you want to fetch data from, table_name is the name of the table you want to retrieve data from, and condition is the filtering criteria.

Examples
Retrieve Rows Based on a Single Condition
SELECT * FROM users
WHERE age > 25;

In the above example, the query selects all rows from the users table where the age is greater than 25.

Retrieve Rows Based on Multiple Conditions
SELECT * FROM products
WHERE category = 'Electronics' AND price > 1000;

The above query selects all rows from the products table where the category is "Electronics" and the price is greater than 1000.

Retrieve Rows with Wildcard Matching
SELECT * FROM employees
WHERE name LIKE 'J%';

This query selects all rows from the employees table where the name starts with "J".

Using Logical Operators
SELECT * FROM orders
WHERE (status = 'pending' OR status = 'processing') AND total_amount > 100;

In this example, the query selects all rows from the orders table with a status of "pending" or "processing" and a total amount greater than 100.

Conclusion

The WHERE clause in MySQL allows you to filter and select specific data from a table based on specified conditions. It enhances the data retrieval process, making it more efficient and targeted. Make use of the various operators and functions available in WHERE clause to construct powerful queries that meet your requirements.