The difference between the group by sort principle of MySQL and sqllite

Source: Internet
Author: User

When we group by on a table record, when aggregate functions such as SUM, Min, max are not explicitly used, the group by collation is compared with MySQL and sqllite

As you all know, when group by, the database is matched to all records that traverse the database table.

The following SQL is intended to query a message in the table GroupID the same record, with the latest time, giving the statement differences between MySQL and Sqllite:

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) T1grou P by T1.groupidorder by T1. ' Created_date ' desc,t1.id DESC

Sqllite statements

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) T1GR OUP by T1.groupidorder by T1.time desc,t1.id desc

Note that compared to SQL, you can find that the subquery uses ASC in the last order by, while in Sqllite we use DESC

The reason for this is that MySQL is ignored when it iterates through the table records when it discovers duplicate group fields. And the sqllite is covered.

So we're using ASC in MySQL to get a record of the latest send, using Desc,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.