MySQL database - MySQL uses DISTINCT to filter duplicate data

When using the SELECT statement to perform a simple data query in MySQL, all matching records are returned. If some fields in the table do not have a unique constraint, then these fields may have duplicate values.

In order to realize the query of non-duplicate data, MySQL provides the  DISTINCT  keyword.

The main function of the DISTINCT keyword is to filter the repeated data of one or more fields in the data table, and return only one piece of data to the user.

The syntax for the DISTINCT keyword is:

SELECT DISTINCT <字段名> FROM <表名>;

Among them, "field name" is the name of the field that needs to eliminate duplicate records, and multiple fields are separated by commas.

There are a few things to keep in mind when using the DISTINCT keyword:

  • The DISTINCT keyword can only be used in a SELECT statement.
  • When deduplicating one or more fields, the DISTINCT keyword must be at the top of all fields.
  • If there are multiple fields after the DISTINCT keyword, multiple fields will be combined to deduplicate, that is, only when the combination of multiple fields is exactly the same will be deduplicated.

Example 1

The following uses a specific example to illustrate how to implement query without duplicate data.

The table structure and data of the student table in the test database are as follows:

mysql> SELECT * FROM test.student;
+----+----------+------+-------+
| id | name     | age  | stuno |
+----+----------+------+-------+
|  1 | zhangsan |   18 |    23 |
|  2 | lisi     |   19 |    24 |
|  3 | wangwu   |   18 |    25 |
|  4 | zhaoliu  |   18 |    26 |
|  5 | zhangsan |   18 |    27 |
|  6 | wangwu   |   20 |    28 |
+----+----------+------+-------+
6 rows in set (0.00 sec)

The result shows that there are 6 records in the student table.

Next, the age field of the student table is deduplicated, and the SQL statement and running results are as follows:

mysql> SELECT DISTINCT age FROM student;
+------+
| age  |
+------+
|   18 |
|   19 |
|   20 |
+------+
3 rows in set (0.00 sec)

To deduplicate the name and age fields of the student table, the SQL statement and running results are as follows:

mysql> SELECT DISTINCT name,age FROM student;
+----------+------+
| name     | age  |
+----------+------+
| zhangsan |   18 |
| lisi     |   19 |
| wangwu   |   18 |
| zhaoliu  |   18 |
| wangwu   |   20 |
+----------+------+
5 rows in set (0.00 sec)

To deduplicate all fields in the student table, the SQL statement and running results are as follows:

mysql> SELECT DISTINCT * FROM student;
+----+----------+------+-------+
| id | name     | age  | stuno |
+----+----------+------+-------+
|  1 | zhangsan |   18 |    23 |
|  2 | lisi     |   19 |    24 |
|  3 | wangwu   |   18 |    25 |
|  4 | zhaoliu  |   18 |    26 |
|  5 | zhangsan |   18 |    27 |
|  6 | wangwu   |   20 |    28 |
+----+----------+------+-------+
6 rows in set (0.00 sec)

Because DISTINCT can only return its target field, but not other fields, so in practice, we often use the DISTINCT keyword to return the number of unique fields.

Query the number of records in the student table after the name and age fields are deduplicated. The SQL statement and running results are as follows:

mysql> SELECT COUNT(DISTINCT name,age) FROM student;
+--------------------------+
| COUNT(DISTINCT name,age) |
+--------------------------+
|                        5 |
+--------------------------+
1 row in set (0.01 sec)

The result shows that there are 5 records in the student table after deduplication of the name and age fields.

Dark horse programmer MySQL database entry to proficiency, from mysql installation to mysql advanced, mysql optimization all covered

Guess you like

Origin blog.csdn.net/Itmastergo/article/details/130282966