zoukankan      html  css  js  c++  java
  • oracle时间比较和分页查询

    条件判断

     <select id="xx" parameterType="Map" resultType="Map">
            select * from (
            select rownum rn, person.client_id as id,
            person.client_id,
            person.name,
            person.cert_type,
            person.cert_no,
            person.birthday,
            person.sex,
            case photo.photo_state
            when '0' then
            '正常'
            when '1' then
            '注销'
            else
            ''
            end as photo_state
            from xx
            left join xo
            on id = t_id
            where te = '0'
            <!-- 判断是否有查询内容-start -->
            <if test="search.certType != null and search.certType != ''">
                AND person.cert_Type=#{search.certType}
            </if>
            <if test="search.certNo != null and search.certNo != ''">
                AND person.cert_no=upper(#{search.certNo})
            </if>
            <if test="search.name != null and search.name != ''">
                AND person.name LIKE concat(concat('%', #{search.name}), '%')
            </if>
            <!-- 判断是否有查询内容-end -->
            <!-- 没有排序条件,自定义默认排序字段 -->
            <choose>
                <when test="order != null and order != ''">
                    ORDER BY ${order}
                </when>
                <!-- 默认按照用户创建时间倒序 -->
                <otherwise>ORDER BY person.open_date desc</otherwise>
            </choose>
            )
            <!-- 分页条件 -->
            where rn &lt;= (#{start} + #{length}) and rn &gt; #{start}
        </select>

    时间比较:

    SELECT REGION_NO,COUNT(*) total
                      FROM xx
                      WHERE 1=1
               <if test="search.beginDate != null and search.beginDate != ''">
                <![CDATA[
                   and SIGN_TIME>to_date(#{search.beginDate},'yyyy-mm-dd hh24:mi:ss')
                ]]>
               </if>
              <if test="search.endDate != null and search.endDate != ''">
                <![CDATA[
                   and SIGN_TIME<to_date(#{search.endDate},'yyyy-mm-dd hh24:mi:ss')
                ]]>
              </if>
              group by REGION_NO ) z
      WHERE z.REGION_NO=d.REGION_NO
    -- ==============================================================================
     where r.client_id = b.client_id and r.data_complete = '0' and r.apply_time >=to_date(#{search.beginDate},'yyyy-MM-dd')
            and r.apply_time<![CDATA[ <= ]]>to_date(concat(#{search.endDate},' 23:59:59'),'yyyy-MM-dd HH24:mi:ss')


    select * from(
    select rownum as rn,e.* from(
    select *,to_char(t.order_time, 'yyyy-mm-dd hh24:mi:ss') order_time,t.biz_id,b.biz_name,bt.trade_name,t.CERT_NO
    from xxt
    WHERE b.zD=t.zx
    --and order_time>to_date('2020-08-30 22:00:00','yyyy-mm-dd hh24:mi:ss')and order_time<to_date('2021-06-10 22:00:00','yyyy-mm-dd hh24:mi:ss')
    )e)
    -- order_time>str_to_date('2019-11-30 22:00:00','yyyy-mm-dd hh24:mi:ss')and order_time<str_to_date(ch, fmt)('2021-06-10 22:00:00','yyyy-mm-dd hh24:mi:ss')
    -- and order_time>to_date('2020-08-30 22:00:00','yyyy-mm-dd hh24:mi:ss')and order_time<to_date('2021-06-10 22:00:00','yyyy-mm-dd hh24:mi:ss')

    分页查询

    <select id="xx" parameterType="Map" resultType="java.util.Map">
            select * from(
             select rownum as rn,e.* from(
                 select t.x,t.x,s.xAS x,t.x,t.x,
                        s1.xAS x,to_char(t.order_time, 'yyyy-mm-dd hh24:mi:ss') order_time,
                        t.biz_id,b.biz_name,bt.trade_name,t.x
                 from xtWHERE b.x=t.xand t.x=#{cert_no}
                      <![CDATA[
                         and order_time>to_date(#{begin_date},'yyyy-mm-dd hh24:mi:ss')
                      ]]>
                      <![CDATA[
                         and order_time<to_date(#{end_date},'yyyy-mm-dd hh24:mi:ss')
                      ]]>
             )e)
            <!-- 分页条件 -->
            where rn &lt;= (#{start} + #{length}) and rn &gt; #{start}
        </select>
    ========================================================================================

    使用:
          --rownum关键字:oracle对外提供的自动给查询结果编号的关键字,与每行的数据没有关系。
            --注意:rownum关键字只能做< <=的判断,不能进行> >=的判断

    
    
     select rownum ,e.* from emp e;
    
    

          --查询员工信息的前5条数据 第一页数据

    
    
     select rownum r,e.* from emp e where rownum <=5;
          select * from (select rownum r,e.* from emp e where rownum <=5) t where r>0;
    
    

          --查询员工信息的6-10条数据 第二页数据

    
    
    
    
    
      select rownum r,e.* from emp e where rownum <=10;
          select rownum,t.* from (select rownum r,e.* from emp e where rownum <=10) t where r>5;
    
    

          --查询员工信息的11-15条数据 第三页数据    

    
    
    select rownum r,e. * from emp e where rownum<=15;
          select * from (select rownum r,e. * from emp e where rownum<=15) t where r>10;
    
    

          --分页规律总结:每页显示m条数据,查询第n页数据  

    
    
      select * from (select rownum r,e. * from 要分页的表 e where rownum<=m*n) t where r>m*n-m ;
    
    

          --要分页的表既可以是真实的表,也可以是一个查询语句

    
    

          --分页查询员工信息按照工资排序

    
    
       select * from (select rownum r,t.* from (select * from emp  order by sal) t where rownum<=10 ) where r>5
     

    注:sql代码不完整

  • 相关阅读:
    C语言I博客作业04
    C语言II博客作业03
    C语言II博客作业02
    C语言II博客作业01
    学期总结
    C语言I博客作业08
    C语言I博客作业07
    C语言I博客作业06
    C语言I博客作业05
    C语言I博客作业04
  • 原文地址:https://www.cnblogs.com/karlz/p/14442790.html
Copyright © 2011-2022 走看看