Differences between Group By sorting principles of MySQL and SQLLite

Source: Internet
Author: User

Differences between Group By 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
) T1
Group by t1.groupid
Order 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. time
From tb_recved_data t where status = 0 order by t. time asc, t. id asc
) T1
Group by t1.groupid
Order 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.

-------------------------------------- Split line --------------------------------------

Install MySQL in Ubuntu 14.04

MySQL authoritative guide (original book version 2nd) Clear Chinese scan PDF

Ubuntu 14.04 LTS install LNMP Nginx \ PHP5 (PHP-FPM) \ MySQL

Build a MySQL Master/Slave server in Ubuntu 14.04

Build a highly available distributed MySQL cluster using Ubuntu 12.04 LTS

Install MySQL5.6 and Python-MySQLdb in the source code of Ubuntu 12.04

MySQL-5.5.38 universal binary Installation

-------------------------------------- Split line --------------------------------------

This article permanently updates the link address:

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.