oracle中rownum的使用

简单的说:oracle中同时使用rownum和order by时,要对子查询用order by,对父查询用rownum

 

代码示例:

<!-- 根据判重时的数据查找ins主键 使用rownum并需要排序的话,必须先用子查询进行排序-->
 <select id="selectPkInsId"
  parameterClass="com.sinosig.premiumServicebus.bean.Client_Message"
  resultClass="java.lang.Long">
  select i.pk_t_ins_id from ( select info.pk_t_ins_id from
  t_se_insurance_info info where info.cont_tel = #body.CONTTEL#
  <dynamic>
   <!-- 是新车 -->
   <isEqual prepend="" property="body.NEWVEHICLE"
    compareValue="1">
    <isNotEmpty prepend="and" property="body.CONTNAME">
     cont_name=#body.CONTNAME#
    </isNotEmpty>
   </isEqual>
   <!-- 非新车 -->
   <isEqual prepend="" property="body.NEWVEHICLE"
    compareValue="0">
    <isNotEmpty prepend="and" property="body.LICENSENO">
     num_plate=#body.LICENSENO#
    </isNotEmpty>
   </isEqual>
   <isNotEqual prepend="and" property="body.agentCode"
    compareValue="00000000">
    substr(info.purgecode,0,2) = 'DM'
   </isNotEqual>
   <isEqual prepend="and" property="body.agentCode"
    compareValue="00000000">
    substr(info.purgecode,0,2) = 'WB'
   </isEqual>
  </dynamic>
  order by info.pk_t_ins_id desc ) i where rownum = 1
 </select>