Home  >  Article  >  Database  >  How to query unique data in MySQL

How to query unique data in MySQL

PHPz
PHPzforward
2023-05-26 10:21:145757browse

1. Query the entire row of unique data

In MySQL, you can use the DISTINCT keyword or GROUP BY clause to query unique data. The DISTINCT keyword acts on the entire row of data, and the GROUP BY clause groups each column of data and returns the results.

  1. Use the DISTINCT keyword

Use the DISTINCT keyword to filter out duplicate entire rows of data. The syntax is as follows:

SELECT DISTINCT column1, column2, ... FROM table_name;

Among them, column1, column2,... represent the column names that need to be filtered for unique data. You can specify one or more columns, and separate multiple columns with commas.

For example, we have a student table (students) as follows:

##1张三20男2李四21Male3 Wang Wu20女##456If we need to query unique student information, we can use the following SQL Statement:
id name age gender
Zhao Liu 22 Female
Qian Qi 21 Male
Sunba 20
SELECT DISTINCT * FROM students;

The execution result is as follows:

id12##3王五20女4赵六#22女5Qian Qi21Male6 Sun Ba20女You can see that using the DISTINCT keyword can filter out duplicate entire rows of data.
name age gender
张三 20 Male
李四 21 Male

Use the GROUP BY clause
  1. Using the GROUP BY clause, you can group data according to columns and return the grouping results. The GROUP BY clause is similar to the DISTINCT keyword. The difference is that the GROUP BY clause can group each column, while the DISTINCT keyword can only act on the entire row of data.
The syntax of the GROUP BY clause is as follows:

SELECT column1, column2, ... FROM table_name GROUP BY column1, column2, ...;
For example, if we need to query unique data based on the age and gender of students, we can use the following SQL statement:

SELECT age, gender FROM students GROUP BY age, gender;
The execution results are as follows:

agegender##20FemaleMaleMale女You can see that using the GROUP BY clause can also achieve the effect of querying non-duplicate data.
20
21
22
2. Query for non-duplicate partial data

Sometimes, in addition to querying for non-duplicate data in the entire row, we also need to query for non-duplicate data in certain columns. data. For this case, we can use subquery or GROUP BY clause.

Use subquery

  1. Use subquery to query on the columns that need to filter non-duplicate data, and pass the IN or NOT IN key Word matches the result with the original table to get the desired result.

  2. For example, if we need to query the unique age information in the student table, we can use the following SQL statement:
SELECT age FROM students WHERE age IN (SELECT DISTINCT age FROM students);

The execution results are as follows:

age##202122
As you can see, subqueries can be used to filter unique data on certain columns and return the required results.
Use GROUP BY clause

  1. Use GROUP BY clause to remove duplicate data when querying some columns. To filter unique data, just specify the corresponding column in the query and then use the column as a parameter of the GROUP BY clause.

    For example, if we need to query the unique gender information in the student table, we can use the following SQL statement:
  2. SELECT gender FROM students GROUP BY gender;
The execution result is as follows:

gender

male As you can see, using the GROUP BY clause can also achieve the function of querying non-duplicate data in some columns.
##female

The above is the detailed content of How to query unique data in MySQL. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete