How to remove duplicate data in a MySQL query?

To remove duplicate data from MySQL query results, you can use the DISTINCT keyword. The specific syntax is as follows:

SELECT DISTINCT 列名 FROM 表名;

The column names refer to the columns that need to be queried, which can be one or multiple columns separated by commas. The table name specifies the table that needs to be queried.

For instance, imagine a table called students, which includes columns for students’ names and ages.

+----+--------+-----+
| id | name   | age |
+----+--------+-----+
| 1  | Alice  | 18  |
| 2  | Bob    | 19  |
| 3  | Alice  | 18  |
| 4  | Carol  | 20  |
+----+--------+-----+

To search for unique names, you can use the following statement:

SELECT DISTINCT name FROM students;

The search results show:

+-------+
| name  |
+-------+
| Alice |
| Bob   |
| Carol |
+-------+

This eliminates duplicate name data.

bannerAds