MySQL Limit ORDER BY

Source: Internet
Author: User

When writing fuzzy queries today, when sorting by time and paging, there is an SQL statement in the MyBatis mapping file

1 SELECT2         <Include refID="Base_column_list"/>3          from USERU4         WHEREU.status!=#{status,jdbctype=VARCHAR}5         <ifTest="Keyword!= NULL  andKeyword!= "'">6              and7             (8U.name like#{keyword,jdbctype=VARCHAR}9             ORU.username like#{keyword,jdbctype=VARCHAR}Ten             ) One         </if> A         ORDER  byU.lastupdateddateDESC -LIMIT #{offsetnum},#{pagesize}

If you sort and then page through, you can get the results you want, but instead

SELECT        <Include refID="Base_column_list"/>         from USERUWHEREU.status!=#{status,jdbctype=VARCHAR}        <ifTest="Keyword!= NULL  andKeyword!= "'">             and(U.name like#{keyword,jdbctype=VARCHAR}            ORU.username like#{keyword,jdbctype=VARCHAR}            )        </if>LIMIT #{offsetnum},#{pagesize}ORDER  byU.lastupdateddateDESC

If you do, you will get an error. The order in which SQL is executed is from where select. Can I think of the Where condition in the same priority who is in front of the priority execution, and the order is the sort of query results, and the limit is the selective acquisition of the query results;

The results are sorted again, and the result should be related to the work mechanism of order by. According to the above examples, it is obvious that the results can be selectively obtained after sorting. If Bo friends can post official document explanation, thank you very much ~

MySQL Limit ORDER BY

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.