What is the default sorting logic when the sorting Field Values in MySQL are equal?

Source: Internet
Author: User
Due to business requirements, several sorting fields are added to the product table. However, the default value of the sorting field (sort) is 0. After a few days, the field value operation of some products has already set the sort value in the background, however, when the remaining products whose sort value is 0 are searched, what logic is used for sorting...

Due to business requirements, several sorting fields are added to the product table. However, the default sorting field (sort) value is 0.

Then, after a few days, the field value operation of some products has already set the sort value in the background, but what logic is the sorting of the remaining products whose sort values are still 0 during retrieval?
PS:auto_idYesprimary keyidIs the unique product number randomly generated by php.

SELECT id,title,auto_id,web_sort FROM `route` WHERE class = 2 AND `status` = 1 AND product_library = 0 ORDER BY web_sort DESC;

Reply content:

Due to business requirements, several sorting fields are added to the product table. However, the default sorting field (sort) value is 0.

Then, after a few days, the field value operation of some products has already set the sort value in the background, but what logic is the sorting of the remaining products whose sort values are still 0 during retrieval?
PS:auto_idYesprimary keyidIs the unique product number randomly generated by php.

SELECT id,title,auto_id,web_sort FROM `route` WHERE class = 2 AND `status` = 1 AND product_library = 0 ORDER BY web_sort DESC;

It should be said that it is based on the primary key order, because the physical order of the data in this table, the query is generally in order to traverse the table. But it's strange why your query results are messy. I tried it locally and it's all in the primary key order.

If you really need a special second sorting requirement, you can add the second sorting rule in the query statement. For example, the sequence is ASC of auto_id:

SELECT id,title,auto_id,web_sort FROM `route` WHERE class = 2 AND `status` = 1 AND product_library = 0 ORDER BY web_sort DESC, auto_id ASC;

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.