MySQL Limit order by, mysqllimit

Source: Internet
Author: User

MySQL Limit order by, mysqllimit

When I write a fuzzy query today and sort by time and pagination, there is such an SQL statement in the ing file of mybatis.

 1 SELECT 2         <include refid="Base_Column_List"/> 3         FROM USER U 4         WHERE U.status != #{status,jdbcType=VARCHAR} 5         <if test="keyword != null and keyword != ''"> 6             AND 7             ( 8             U.NAME LIKE #{keyword,jdbcType=VARCHAR} 9             OR U.USERNAME LIKE #{keyword,jdbcType=VARCHAR}10             )11         </if>12         ORDER BY U.lastupdateddate DESC13         LIMIT #{offsetNum},#{pageSize}

If the results are sorted first and then paged, you can get the desired results, but vice versa.

SELECT        <include refid="Base_Column_List"/>        FROM USER U        WHERE U.status != #{status,jdbcType=VARCHAR}        <if test="keyword != null and keyword != ''">            AND            (            U.NAME LIKE #{keyword,jdbcType=VARCHAR}            OR U.USERNAME LIKE #{keyword,jdbcType=VARCHAR}            )        </if>        LIMIT #{offsetNum},#{pageSize}        ORDER BY U.lastupdateddate DESC

In this case, an error is reported. The SQL Execution sequence is from where select. Can I think that, under the where condition with the same priority, who is in the front will give priority to execution, while order is the sorting of query results, limit is the selective acquisition of query results; if limit is in the front, the better results are after selective acquisition

The result is then sorted. Whether the result can be obtained is related to the order by working mechanism. According to the above example, we can only sort and then selectively obtain the results. Thank you very much if you can post an explanation in the official document ~

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.