分頁數據有重復的問題


前段時間,測試給了個bug ,前端顯示時第一頁和第二頁有重復的數據,后台debug測試也有。把控制台打印的sql考到可視化工具跑一下,問題還是有。

然后再去除分頁,發現問題就沒有了。出現問題點在於分頁。后來請教了我們公司的大佬。說可能是因為你那個字段值有重復的情況,最好加一個可以保證唯一性的字段對數據進行排序。

之前沒改的sql

select * from(
        select
        <include refid="Base_Column_List"/>,
        case
        when TRAIN_STATUS=1
        then '1'
        when TRAIN_STATUS=2
        then '2'
        when TRAIN_STATUS=3
        then '3'
        when TRAIN_STATUS=5
        then '5'
        when (to_char(TRAIN_START_DATE, 'yyyy-mm-dd') || TRAIN_START_TIME) >
        to_char(sysdate, 'yyyy-mm-ddHH24:mi')
        then '4-1'
        when (to_char(TRAIN_END_DATE, 'yyyy-mm-dd') || TRAIN_END_TIME) >
        to_char(sysdate, 'yyyy-mm-ddHH24:mi')
        then '4-2'
        else '4-3'
        end as STATUS
        from TB_TRAINING_INFO
        order by
        case
        when STATUS='1'
        then 1
        when STATUS='3'
        then 2
        when STATUS='4-1'
        then 3
        when STATUS='4-2'
        then 4
        when STATUS='4-3'
        then 5
        when STATUS='2'
        then 6
        else 7
        end asc
        )
        <where>
            <if test="trainName!=null and trainName!=''">
                and TRAIN_NAME like '%' || #{trainName,jdbcType=OTHER} ||'%'
            </if>
            <if test="originatorName!=null and originatorName!=''">
                and ORIGINATOR_NAME like '%' || #{originatorName,jdbcType=OTHER} || '%'
            </if>
            <if test="originatorOrgName!=null and originatorOrgName!=''">
                and ORIGINATOR_ORG_NAME like '%' || #{originatorOrgName,jdbcType=OTHER} || '%'
            </if>
            <if test="trainStatus!=null and trainStatus!=''">
                and TRAIN_STATUS = #{trainStatus,jdbcType=CHAR}
            </if>
            <if test="originator!=null and originator!=''">
                and ORIGINATOR = #{originator,jdbcType=OTHER}
            </if>
            <if test="status!=null and status!=''">
                and STATUS = #{status,jdbcType=OTHER}
            </if>
            and delete_flag='0'
        </where>

從上面sql得到status他的值是有重復的,然后我再加一個字段問題解決了。附上修改sql

select * from(
        select
        <include refid="Base_Column_List"/>,
        case
        when TRAIN_STATUS=1
        then '1'
        when TRAIN_STATUS=2
        then '2'
        when TRAIN_STATUS=3
        then '3'
        when TRAIN_STATUS=5
        then '5'
        when (to_char(TRAIN_START_DATE, 'yyyy-mm-dd') || TRAIN_START_TIME) >
        to_char(sysdate, 'yyyy-mm-ddHH24:mi')
        then '4-1'
        when (to_char(TRAIN_END_DATE, 'yyyy-mm-dd') || TRAIN_END_TIME) >
        to_char(sysdate, 'yyyy-mm-ddHH24:mi')
        then '4-2'
        else '4-3'
        end as STATUS
        from TB_TRAINING_INFO
        order by
        case
        when STATUS='1'
        then 1
        when STATUS='3'
        then 2
        when STATUS='4-1'
        then 3
        when STATUS='4-2'
        then 4
        when STATUS='4-3'
        then 5
        when STATUS='2'
        then 6
        else 7
        end asc,UPDATE_DATE desc
        )
        <where>
            <if test="trainName!=null and trainName!=''">
                and TRAIN_NAME like '%' || #{trainName,jdbcType=OTHER} ||'%'
            </if>
            <if test="originatorName!=null and originatorName!=''">
                and ORIGINATOR_NAME like '%' || #{originatorName,jdbcType=OTHER} || '%'
            </if>
            <if test="originatorOrgName!=null and originatorOrgName!=''">
                and ORIGINATOR_ORG_NAME like '%' || #{originatorOrgName,jdbcType=OTHER} || '%'
            </if>
            <if test="trainStatus!=null and trainStatus!=''">
                and TRAIN_STATUS = #{trainStatus,jdbcType=CHAR}
            </if>
            <if test="originator!=null and originator!=''">
                and ORIGINATOR = #{originator,jdbcType=OTHER}
            </if>
            <if test="status!=null and status!=''">
                and STATUS = #{status,jdbcType=OTHER}
            </if>
            and delete_flag='0'
        </where>

總結:order by的時候最后在目標排序字段的基礎上應該加上一個可以保證唯一性的字段對數據進行排序


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM