Home >Database >Mysql Tutorial >How to query unique data in MySQL

How to query unique data in MySQL

PHPz
PHPzOriginal
2023-04-21 10:10:258393browse

MySQL is currently the most popular relational database management system and is widely used in the field of data storage and processing. In practical applications, querying unique data is a common requirement. This article will introduce how to query unique data in MySQL.

1. Query the entire row of non-duplicate data

In MySQL, you can use the DISTINCT keyword or GROUP BY clause to query the non-duplicate data, where the DISTINCT keyword acts on the entire row of data. , the GROUP BY clause groups each column of data and returns the result.

  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 and column2 ,... indicates the column names that need to be filtered for unique data. You can specify one or more columns, and multiple columns are separated by 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. Use the GROUP BY clause to group each column of data and return the 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. 2. Query the non-duplicate partial data
20
21
22

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

Use subquery

    Use subquery to query on the columns that need to filter non-duplicate data, and use the IN or NOT IN keyword to compare the results with the original table match to get the desired result.
  1. 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##202122Use GROUP BY clause
As you can see, subqueries can be used to filter unique data on certain columns and return the required results.

Using GROUP BY clause can also realize the function of querying non-duplicate data in some columns. You only need to specify the columns that need to filter unique data in the query, and then use them as parameters of the GROUP BY clause.
  1. 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. To sum up, querying non-duplicate data is a frequently used operation in MySQL, which can be achieved using the DISTINCT keyword, GROUP BY clause and subquery. In practical applications, we can choose different methods to query unique data according to needs.
##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:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn