The basic usage tutorial of Distinct and Group By statements in MySQL,

Source: Internet
Author: User

The basic usage tutorial of Distinct and Group By statements in MySQL,

Remove duplicate query results from MySQL Distinct
DISTINCT
You can use the DISTINCT keyword to remove duplicate records of a field in the query.
Syntax:

SELECT DISTINCT(column) FROM tb_name


Example:
Assume that the user table has the following records:

Uid username1 Xiao Li 2 Xiao Zhang 3 Xiao Li 4 Xiao Wang 5 Xiao Li 6 Xiao Zhang

SQL statement:
Select distinct (username) FROM user
The returned query result is as follows:

Username Xiao Li Xiao Zhang Xiao Wang

Prompt
Removing duplicate records using the DISTINCT keyword has great limitations. DISTINCT () can contain only one field, and the query result returns only this field, not the complete data record (as shown in the preceding example ).
Try the following syntax:

SELECT DISTINCT(column),column1,column2,... FROM tb_name

The query results will return all the listed fields, but this query often invalidates the uniqueness of column, and column 1, column 2 ,... It cannot be placed before DISTINCT (column.
In the preceding example, if you want to return the following results (this is often expected ):

Uid username1 Xiao Li 2 Xiao Zhang 3 Xiao Wang

In this case, the group by keyword is used.

MySQL Group By Data Group
GROUP
In MySQL, the group by keyword is used to query a GROUP for a field or some fields, and the first record of repetition is returned.
Syntax:

SELECT column,... FROM tb_name GROUP BY column1,column2 ...

The user table records are as follows:

Uid username1 Xiao Li 2 Xiao Zhang 3 Xiao Li 4 Xiao Wang 5 Xiao Li 6 Xiao Zhang

The preceding user table is queried as follows:

SELECT * FROM user GROUP BY username

The returned query result is as follows:

Uid username1 Xiao Li 2 Xiao Zhang 3 Xiao Wang

Description
The usage of group by syntax in MySQL database is quite different from that in other databases. For standard SQL statements, GROUP BY must be used in combination with Aggregate functions. In addition to aggregate functions, the selected fields must also appear in GROUP. However, the group by function is extended in MySQL:
Without an aggregate function, the returned result is the first row in the group by result set, as shown in the preceding example.
When group by is combined with Aggregate functions, the selected field does not need to exist in group by. MySQL has the implicit field function.
Therefore, we can easily obtain the desired query results based on MySQL's extended features of group by and some other keywords such as order.
Example 2:

SELECT * FROM user GROUP BY username,uid

The returned query result is as follows:

Uid username1 Xiao Li 3 Xiao Li 5 Xiao Li 2 Xiao Zhang 6 Xiao Zhang 4 Xiao Wang

Articles you may be interested in:
  • Performance Comparison between distinct and group by in MySQL
  • Parsing mysql: removing duplicate records for single-Table distinct and multi-Table group by queries

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.