The problem of order by when paginating

      Made a pagination, sorted by sampling time.

Select * from (select ROWNUM RN,X.*
        from (
            select s.* from T_TEST_SAMPLE s
            <where> 
                <if test="qualityName!=null and qualityName!=''">
                    AND TRIM(s.sample_name) = #{qualityName,jdbcType=VARCHAR}
                </if>
                <if test="begin!=null ">
                    and SAMPLING_DATE &gt;= #{begin,jdbcType=TIMESTAMP}
                </if>
                <if test="end!=null ">
                    AND SAMPLING_DATE &lt;= #{end,jdbcType=TIMESTAMP}
                </if>
            </where>
            order by s.SAMPLING_DATE desc
        ) X
        where ROWNUM &lt;= #{endIndex,jdbcType=INTEGER}
        ) Z
        where RN &gt; #{offset,jdbcType=INTEGER}

      Recently, the business process has changed, and the sampling time is equal.

 

      Later, after querying the information for several days, a situation appeared. The last two of the third page appear on the first two of the fourth page.

      Later, I thought about it. It is estimated that because the data with equal sampling time is too much, the sorting is messed up, and an impossible repeating field sorting is used.

            order by s.SAMPLING_DATE desc,id desc

      This situation is normal ~

 

      This article is very good:

https://www.cnblogs.com/lwlxqlccc/p/8676045.html

Published 48 original articles · Like 36 · Visits 130,000+

Guess you like

Origin blog.csdn.net/weixin_42845682/article/details/88368648