zoukankan      html  css  js  c++  java
  • postgresql数据库中的 rownum

    公司使用的是postgresql数据库

    今天在写项目的时候,遇到了这样一个问题,自己写出的sql语句,如果一次性全部查询出来一共有24条数据,这24条数据也是按照bussinessdate进行降序排列的,显示的最近日期就是自己今天操作的,2020-06-11

    但是如果自己加上分页,控制每页显示10条的化,就虽然查询出来的结果也是按照降序排列的,但是6-11号的数据就没有显示,很是奇怪,最后询问同事,解决了问题:

    原先的代码:

        <select id="queryScoreDetailList" parameterType="com.picc.hmbms.outinterface.scoreDetail.vo.ScoreDetailInfoQueryVo" resultMap="reMemberScoreInfo_i">
            select *
            from (select tmp_page.*
            from (
            select distinct tsg.member_code   memberCode,
            row_number() OVER() as rownum,
            tsg.SCORE_TYPE  ,
            <!--  tsd.CONSUMEORG         businessOrg, -->
            <!-- tsd.GRANTORG         grantOrg,  -->
            tsd.GRENT_PROJECT,
            to_char(tsd.insert_time,'yyyy-MM-dd') insertTime,
            to_char(tsg.end_date,'yyyy-MM-dd') endDate,
            tss.order_no orderNo,
            tsg.content,
            (case when tsd.consumeorg is null then tsd.grantorg else tsd.consumeorg end ) businessOrg,
            tsd.SYS_NO         sysNo ,
            (case when tsd.SCORE is  null  then 0  else round(tsd.SCORE,0) end)     score,
            (case when tss.consumer_date is null then to_char(tsg.GRANT_DATE,'yyyy-MM-dd hh24:mi:ss')
            else  to_char(tss.consumer_date,'yyyy-MM-dd hh24:mi:ss') end) businessDate,
            tsd.BUSINESS_TYPE businessType
            from t_member_score_detail tsd
            left join t_score_grent tsg  on tsg.Member_Code = tsd.member_code   and tsg.SCORE_GRENT_ID = tsd.SCORE_GRENT_ID
            left join t_score_settel tss on tss.Member_Code = tsd.member_code   and tss.SCORE_SETTEL_ID = tsd.SCORE_SETTEL_ID
            <if test="memberCode != null and '' != memberCode">
                where tsd.MEMBER_CODE = #{memberCode, jdbcType=VARCHAR}
            </if>
            <if test="startDate != null and ''!= startDate">
                AND ( to_date(to_char(tsd.GRANT_DATE, 'yyyy-MM-dd'), 'yyyy-MM-dd') <![CDATA[ >= ]]>to_date(#{startDate},'yyyy-MM-dd')
                or to_date(to_char(tsd.CONSUME_DATE, 'yyyy-MM-dd'), 'yyyy-MM-dd')<![CDATA[ >= ]]> to_date(#{startDate},'yyyy-MM-dd'))
    
            </if>
            <if test="endDate != null and '' != endDate">
                AND ( to_date(to_char(tsd.GRANT_DATE, 'yyyy-MM-dd'), 'yyyy-MM-dd') <![CDATA[ <= ]]> to_date(#{endDate},'yyyy-MM-dd')
                or to_date(to_char(tsd.CONSUME_DATE, 'yyyy-MM-dd'), 'yyyy-MM-dd') <![CDATA[ <= ]]> to_date(#{endDate},'yyyy-MM-dd'))
            </if>
            order by businessDate desc ) tmp_page
            where rownum <![CDATA[ <= ]]> #{maxNum} and  tmp_page.businessDate is not null) lm
            where rownum > #{minNum}
      </select>

    修改之后的代码:

    <select id="queryScoreDetailList" parameterType="com.picc.hmbms.outinterface.scoreDetail.vo.ScoreDetailInfoQueryVo" resultMap="reMemberScoreInfo_i">
            select *
            from (select tmp_page.*
            from (
            select distinct tsg.member_code   memberCode,
            row_number() OVER (order by consumer_date )   AS    rownum,
            tsg.SCORE_TYPE  ,
            <!--  tsd.CONSUMEORG         businessOrg, -->
            <!-- tsd.GRANTORG         grantOrg,  -->
            tsd.GRENT_PROJECT,
            to_char(tsd.insert_time,'yyyy-MM-dd') insertTime,
            to_char(tsg.end_date,'yyyy-MM-dd') endDate,
            tss.order_no orderNo,
            case when tsd.score_type='06' then '挑战赢积分活动扣100积分' else tsg.CONTENT end,
            (case when tsd.consumeorg is null then tsd.grantorg else tsd.consumeorg end ) businessOrg,
            tsd.SYS_NO         sysNo ,
            (case when tsd.SCORE is  null  then 0  else round(tsd.SCORE,0) end)     score,
            (case when tss.consumer_date is null then to_char(tsg.GRANT_DATE,'yyyy-MM-dd hh24:mi:ss')
            else  to_char(tss.consumer_date,'yyyy-MM-dd hh24:mi:ss') end) businessDate,
            tsd.BUSINESS_TYPE businessType
            from t_member_score_detail tsd
            left join t_score_grent tsg  on tsg.Member_Code = tsd.member_code   and tsg.SCORE_GRENT_ID = tsd.SCORE_GRENT_ID
            left join t_score_settel tss on tss.Member_Code = tsd.member_code   and tss.SCORE_SETTEL_ID = tsd.SCORE_SETTEL_ID
            <if test="memberCode != null and '' != memberCode">
                where tsd.MEMBER_CODE = #{memberCode, jdbcType=VARCHAR}
            </if>
            <if test="startDate != null and ''!= startDate">
                AND ( to_date(to_char(tsd.GRANT_DATE, 'yyyy-MM-dd'), 'yyyy-MM-dd') <![CDATA[ >= ]]>to_date(#{startDate},'yyyy-MM-dd')
                or to_date(to_char(tsd.CONSUME_DATE, 'yyyy-MM-dd'), 'yyyy-MM-dd')<![CDATA[ >= ]]> to_date(#{startDate},'yyyy-MM-dd'))
    
            </if>
            <if test="endDate != null and '' != endDate">
                AND ( to_date(to_char(tsd.GRANT_DATE, 'yyyy-MM-dd'), 'yyyy-MM-dd') <![CDATA[ <= ]]> to_date(#{endDate},'yyyy-MM-dd')
                or to_date(to_char(tsd.CONSUME_DATE, 'yyyy-MM-dd'), 'yyyy-MM-dd') <![CDATA[ <= ]]> to_date(#{endDate},'yyyy-MM-dd'))
            </if>
            order by businessDate desc ) tmp_page
            where rownum <![CDATA[ <= ]]> #{maxNum} and  tmp_page.businessDate is not null) lm
            where rownum > #{minNum}
      </select>

    总的来说:一定要在over之后添加上排序的条件。

  • 相关阅读:
    springboot中使用swagger
    springboot中使用druid数据库连接池
    springboot连接mysql时出现的错误
    请求路径springmvc中支持ant风格
    mybatis中的<where>和<set>
    spring中Bean装配有关的注解
    spring在web项目中ApplicationContext容器的实例化
    mybatis中resultMap的作用
    mybatis中的别名
    mysql利用LAST_INSERT_ID实现id生成器
  • 原文地址:https://www.cnblogs.com/dongyaotou/p/13096689.html
Copyright © 2011-2022 走看看