How to query unique data in MySQL

PHPz
Release: 2023-05-26 10:21:14
forward
5690 people have browsed it

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;
Copy after login

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 李四 21 Male 3 Wang Wu 20 女 ##4 5 6 If 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;
Copy after login

The execution result is as follows:

id 1 2 ##3 王五 20 女 4 赵六 #22 女 5 Qian Qi 21 Male 6 Sun Ba 20 女 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, ...;
Copy after login
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;
Copy after login
The execution results are as follows:

age gender ##20 Female Male Male 女 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);
Copy after login

The execution results are as follows:

age ##20 21 22
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;
    Copy after login
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!

Related labels:
source:yisu.com
Statement of this Website
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
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!