A large amount of data has been collected on a site, and about 100,000 of the data has been collected on a site in the same industry. Of course, the title has not been changed, or the original title. This is to facilitate the filtering of duplicate data. These hundreds of thousands of data records contain a large proportion of duplicate data, which was originally intended to be deleted, however, the summer blog failed to find a good method, and finally thought of the distinct parameter in SQL. This parameter has not been used for a long time. Its role is to filter out duplicate data and not display it, it seems that it is most appropriate to use it in this collection site. Next we will focus on the application of distinct in SQL.
Role of distinct:
In a data table, many values are often repeated. For example, if we only want to output a "summer Blog", we can use distinct, with distinct, only one "summer blog" can be displayed ".
Distinct syntax:
Because the output value is required, SELECT is required. The syntax is as follows:
Select distinct column name FROM table name
Example 1 of distinct:
To more intuitively see the effects of distinct, let's take a look at the effects before and after distinct. For example, we have the following Orders table:
If you do not use distinct to read data, the SQL statement is the "SELECT Company FROM Orders" statement, and the result is as follows:
We can see that the duplicate value W3School is displayed twice. If the content is repeated, it can be displayed only once, next, let's take a look at the value output after distinct. Let's take a look at the SQL statement after distinct is used, as shown below:
Select distinct Company FROM Orders
The result format after running is:
Currently, W3School is only displayed once.
Example 2 of distinct:
A simple small instance, if we have the following table:
Use distinct in SQL to remove duplicate data. The effect after running is as follows:
Example 3 of distinct:
Next, let's change the SQL statement slightly, not only remove the duplicate values in the table name, but also remove the ids. The SQL statement is as follows:
Select distinct name, id from
The following figure shows the effect after execution:
We can see that the repeated values in the name field and id field have been removed from the execution result.