MySQL Where
On this page
PHP MySQL Select Where (PHP MySQL选择位置)
The SELECT statement is used to fetch data from a database. The WHERE clause is used to filter data based on specific conditions. The WHERE clause can be combined with the SELECT statement to fetch only specific data from the database. (SELECT语句用于从数据库获取数据。WHERE子句用于根据特定条件筛选数据。WHERE子句可以与SELECT语句组合,以仅从数据库获取特定数据。)
Here is the basic syntax of the SELECT statement with WHERE clause:
SELECT column1, column2, ...
FROM table_name
WHERE condition;
In the above syntax:
column1, column2, … are the columns you want to fetch data from. (- column1, column2,…是您要从中获取数据的列。)
table_name is the name of the table you want to fetch data from. (- table_name是要从中获取数据的表的名称。)
condition is a logical expression that determines what data to retrieve. (- condition是一个逻辑表达式,用于确定要检索的数据。)
Example
Example (示例)
Consider a table named customers with the following data:
id | name | country | |
---|---|---|---|
1 | John | [email protected] | USA |
2 | Jane | [email protected] | UK |
3 | Alice | [email protected] | France |
Here is an example of how to use the SELECT statement with WHERE clause to retrieve only specific data from the customers table:
SELECT name, email
FROM customers
WHERE country = 'USA';
The above SELECT statement will return only the rows where the country column is equal to USA. The output will be:
name | |
---|---|
John | [email protected] |
Multiple Conditions
Multiple Conditions (多个条件)
You can also use multiple conditions in the WHERE clause. For example, you can retrieve all customers from the customers table who live in the USA and have an email address that ends with example.com. (您还可以在WHERE子句中使用多个条件。例如,您可以从customers表中检索居住在美国且电子邮件地址以example.com结尾的所有客户。)
SELECT name, email
FROM customers
WHERE country = 'USA'
AND email LIKE '%example.com';
The above SELECT statement will return the following output:
name | |
---|---|
John | [email protected] |
You can use logical operators such as AND and OR to combine multiple conditions in the WHERE clause.