SQL WHERE
The WHERE clause is used to filter records.
SQL WHERE clause
The WHERE clause is used to extract those records that meet the specified criteria.
SQL WHERE syntax
SELECT column_name,column_name
FROM table_name
WHERE column_name operator value;
FROM table_name
WHERE column_name operator value;
Demo database
In this tutorial, we will use the php sample database.
The following is the data selected from the "Websites" table:
+----+--------------+--- ------------------------+-------+---------+
| id | name | url --------+-------+---------+
| 1 | Google | https://www.google.cm/ | 1 | USA |
| 2 | Taobao | https://www.taobao.com/ | 13 | CN |
| 3 | php Chinese website | http://www.php.cn/ | 4689 | CN |
| 4 | Weibo | http://weibo.com/ | 20 | CN |
| 5 | Facebook | https://www.facebook.com/ | 3 | USA |
+--- -+-------------+--------------------------+----- --+---------+
| id | name | url --------+-------+---------+
| 1 | Google | https://www.google.cm/ | 1 | USA |
| 2 | Taobao | https://www.taobao.com/ | 13 | CN |
| 3 | php Chinese website | http://www.php.cn/ | 4689 | CN |
| 4 | Weibo | http://weibo.com/ | 20 | CN |
| 5 | Facebook | https://www.facebook.com/ | 3 | USA |
+--- -+-------------+--------------------------+----- --+---------+
WHERE clause exampleThe following SQL statement selects the country "CN" from the "Websites" table "All websites:
ExampleSELECT * FROM Websites WHERE country='CN';Execution output result:
Text fields vs. numeric fieldsSQL uses single quotes to surround text values (most database systems also accept double quotes quotation marks). In the previous example, single quotes were used in the 'CN' text field. If it is a numeric field, please do not use quotation marks.
ExampleSELECT * FROM Websites WHERE id=1;
##Operators in the WHERE clause The following operators can be used in the WHERE clause:
Execution output result:
##Operators in the WHERE clause The following operators can be used in the WHERE clause:
Description | |
---|---|
is equal to | |
is not equal to. | Note: In some versions of SQL, this operator can be written as != |
is greater than | |
is less than | |
is greater than or equal to | |
Less than or equal to | |
Within a certain range | |
Search for a pattern | |
Specify multiple possible values for a column |