Comparison and usage of distinct and group by statements in MySQL, mysqldistinct

Source: Internet
Author: User

Comparison and usage of distinct and group by statements in MySQL, mysqldistinct

The data table records the bibliography used for user verification. Now I want to retrieve all the bibliography and use DISTINCT and group by to get the expected results, but I found that the returned results are arranged differently, distinct is displayed in the order of data storage, while group by is sorted (generally ASC ).
 
DISTINCT is actually very similar to the implementation of the group by operation, except that only one record is retrieved in each GROUP after group. Therefore, the implementation of DISTINCT is similar to that of group by, and there is no big difference. It can also be achieved through loose index scanning or compact index scanning.
 
Which of the following is more efficient for DISTINCT and group?
 
The DISTINCT operation only needs to find all the different values. The group by operation also requires preparation for other Aggregate functions. At this point, the group by operation should do more work than DISTINCT.
 
But in fact, group by is more efficient. Why? For the DISTINCT operation, it reads all records, and the number of records to be read by group by is as large as the number of groups in the GROUP, that is, it is much less than the actual number of records.

The following describes how to use distinct and group by in MySQL.

CREATE TABLE `student` (                `name` varchar(20) NOT NULL DEFAULT '',       `age` int(10) DEFAULT '0'             ) ENGINE=InnoDB DEFAULT CHARSET=latin1

1. Test 1

select * from student;   


a  5a  5c  0

Use distinct to filter out records with the same columns

select distinct name,age from student;

Return

a  5c  0

2. Test 2
Change the data of student to the following:

select * from student;
c  2c  5
select distinct name,age from student;

The following is returned, indicating that when there are more than one column of fields after distinct, only the values of each column are identical.

c  2c  5

3. Test 3

select * from student;
name age heightc  2  123c  2  456b  20  222

Group by groups by two columns at the same time

select name,age,sum(height) from student group by name,age;
b  20  222c  2  579

Group by groups two columns at the same time, and adds the having condition to the end.

select name,age,sum(height) as n from student group by name,age having n > 500;
 

Return
C 2 579

4. Test 4
Test the limit operation after group

Copy codeThe Code is as follows:
Select songname, sengerid, count (sengerid) as n from t_song group by songname, sengerid having n> 1 order by n DESC, songid ASC limit 10;

Unknown 8738 40 share over 1432 24 wind continues to blow 1432 23 ghost 1432 23 unintentional sleep 1432 23 Luo Baiji super hi party continuous concert 780 19 refuse to play 1432 19 wind then start again 1432 18 love you some more 1480 every day 18 1794 million words 18

Copy codeThe Code is as follows:

Select songname, sengerid, count (sengerid) as n from t_song group by songname, sengerid having n> 1 order by n DESC, songid ASC limit 5;


Unknown 8738 40 spent 1432 24 wind continues to blow 1432 23 ghost 1432 23 unintentional sleep 1432 23

After the above two tests, we can see that if the SQL statement contains limit, limit groups the group by statement and performs the related calculated limit operations, instead of grouping the specified number of records after limit, we can see that the value of each row of data in the n column is greater than 10.

5. Test 5
In the following two forms of distinct, the same number of records can be obtained. The results are the same as the number of records written differently.

select count(distinct(songid)) from feedback;select count(distinct songid) from feedback;

6. Test 6
Field singername is string, max (singername). If some singername columns are empty and some are not empty, max (singername) takes a non-empty value. If one column is zxx, if the column is lady, zxx is used and the letter is followed.

Copy codeThe Code is as follows:
Select feedback_id, songid, songname, max (singername), max (time) as new_time from feedback group by songid order by new_time desc;


7. SQL statement where, group by, order by, and limit order

where xxx,group by xxx,order by xxx,limit xxx

8. Questions about group by and count
If the SQL statement contains group by, it is better not to convert count SQL to select count (*) from xxx. Otherwise, the fields between select and from may be used later. For example:

Copy codeThe Code is as follows:
Select feedback_id, songid, songname, max (singername), max (time) as new_time from feedback group by songid order by new_time desc;


Copy codeThe Code is as follows:

MySQL Query Error: select count (*) FROM feedback group by songid order by new_time DESC Error Info: Unknown column 'new _ time' in 'order clause'


Articles you may be interested in:
  • Basic Principles of distinct statements in MySQL and comparison with group by statements
  • Optimize the distinct statement and multi-field operations of distinct in MySQL Indexes
  • Query duplicate records using distinct statements in MySQL and Performance Discussion
  • How to Use Distinct and Group By statements in MySQL
  • Comparison of distinct and count (*) in MySQL
  • Performance Comparison between distinct and group by in MySQL
  • Tips for optimizing distinct in MySQL
  • Parsing mysql: removing duplicate records for single-Table distinct and multi-Table group by queries
  • How to Use distinct to query multiple unique record values in mysql

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.