Differences between the GroupBy sorting principles of MySQL and SQLLite

Source: Internet
Author: User

Differences between the GroupBy sorting principles of MySQL and SQLLite

When we perform group by on the records of a table and do not explicitly use aggregate functions such as sum, min, and max, the group by sorting rules are as follows:

As we all know, when using group by, the database traverses all records in the database table for matching.

The following SQL statement is used to query a message of the latest time in the same groupid record in the table. The statement differences between mysql and sqllite are given:

MYSQL statement

SELECT * FROM(SELECT t.id,t.data_id,t.send_username,t.recv_username,t.message,IFNULL(NULLIF(t.groupid,''),t.recv_username) AS groupid,t.`created_date`FROM tb_push_data t WHERE STATUS = 0 ORDER BY t.`created_date` desc,t.id desc) t1GROUP BY t1.groupidORDER BY t1.`created_date` DESC,t1.id DESC
SQLLite statement
select * from(select t.id,t.data_id,t.login_username,t.send_username,t.recv_username,t.message,ifnull(nullif(t.groupid,''),t.send_username) as groupid,t.timefrom tb_recved_data t where status = 0 order by t.time asc, t.id asc) t1group by t1.groupidorder by t1.time desc,t1.id desc

When comparing SQL statements, we can find that ASC is used in MYSQL in the ORDER BY statement at the end of the subquery, and DESC is used in SQLLite.

This is because MYSQL ignores repeated group fields when traversing table records. SQLLite is covered.

Therefore, desc is used in mysql to obtain a record sent at the latest, and asc is used in sqllite.

Related Article

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.