SQL SELECT DISTINCT
The SELECT DISTINCT statement is used to return uniquely different values.
SQL SELECT DISTINCT statement
In a table, a column may contain multiple duplicate values, and sometimes you may want to list only distinct values.
The DISTINCT keyword is used to return uniquely different values.
SQL SELECT DISTINCT syntax
SELECT DISTINCT column_name,column_name
FROM table_name;
FROM table_name;
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 |
+--- -+-------------+--------------------------+----- --+---------+
SELECT DISTINCT ExampleThe following SQL statement only selects the "country" from the "Websites" table " Select the only different value in the column, that is, remove the duplicate values in the "country" column:
ExampleSELECT DISTINCT country FROM Websites;Output results: