sql查詢所有數據並把中間數據靠前

mysql> select * from Test ORDER BY case name when 2 then 1 else 2 END,id desc;
+----+------+
| id | name |
+----+------+
|  3 |    2 |
|  4 |    1 |
|  2 |    3 |
|  1 |    4 |
+----+------+
4 rows in set

select * from(select * from lssuing l where 1=1 and state=2 order by case when lssuingType=5 then 0 when lssuingType<>5 then 1 else 2 end,borrowTime asc) a union all select * from(select * from lssuing l where 1=1 and state<>2 order by case  when state=1 then 1 when state=3 then 2  when state=4 then 3  else 3 end ,borrowTime desc) b

sql="select * from("+sql+" and state=2 order by case when lssuingType=5 then 0 when lssuingType<>5 then 1 else 2 end,borrowTime asc) a union all select * from("+sql+" and state<>2 order by case  when state=1 then 1 when state=3 then 2  when state=4 then 3  else 3 end ,borrowTime desc) b";


    HttpServletRequest request=ServletActionContext.getRequest();
        String time = request.getParameter("time");//時間
        String rate = request.getParameter("rate");//利率 
        String order = request.getParameter("order");//排序
        System.out.println("order="+order);
        String sql ="select * from lssuing l where 1=1";
        if(!"".equals(this.lussingType) && this.lussingType!=0){
            sql =sql+ " and l.lssuingType=" + this.lussingType;
        }
        if(time!=null && !"".equals(time)){
            if("1".equals(time)){//0~3個月
                sql =sql+" and (l.periodTimeId <=3 or l.periodDayId is not null)";
            }else if("2".equals(time)){//3~6個月
                sql =sql+" and (l.periodTimeId <=6 and l.periodTimeId>=3)";
            }else{
                sql =sql+" and l.periodTimeId >=6 ";
            }
            ActionContext.getContext().put("time", time);
        }
        if(rate!=null && !"".equals(rate)){
            if("1".equals(rate)){//利率爲10~13
                sql=sql+" and ((l.rate>=10 and l.rate<=13 and l.periodTimeId is not null) or " +
                        "(l.periodDayId is not null and ((l.rate *360)>=10 and (l.rate *360)<=13)))";
            }else if("2".equals(rate)){
                sql=sql+" and ((l.rate>=13 and l.rate<=15 and l.periodTimeId is not null) or " +
                        "(l.periodDayId is not null and ((l.rate *360)>=13 and (l.rate *360)<=15)))";
            }else{
                sql=sql+" and ((l.rate>=15  and l.periodTimeId is not null) or " +
                        "(l.periodDayId is not null and ((l.rate *360)>=15)))";
            }
            ActionContext.getContext().put("rate", rate);
        }
        if(order!=null && !"".equals(order)){
            if(order.equals("2")){
            sql=sql+" order by l.borrowTime desc";
            }else if(order.equals("1")){
            sql=sql+" order by -l.borrowMoney desc";
            }
            ActionContext.getContext().put("order", order);
        }else{
            //!"".equals(this.lussingType) && this.lussingType!=0
            sql="select * from("+sql+" and state=2 order by case when lssuingType=5 then 0 when lssuingType<>5 then 1 else 2 end,borrowTime asc) a union all select * from("+sql+" and state<>2 order by borrowTime desc) b";
        }
        System.out.println("sql================================="+sql);
        List lssuings = this.lssuingService.findLssuingsBySearch(sql);
        int totalRecord =lssuings.size();
        if(totalRecord % this.pageSize==0){
            this.totalPage=totalRecord / this.pageSize;
        }else {
            this.totalPage=(totalRecord / this.pageSize)+1;
        }
        if(this.currentPage >= this.totalPage){
            this.currentPage=this.totalPage;
        }
        if(this.currentPage <=1){
            this.currentPage=1;
        }
        sql=sql+" limit "+(this.currentPage-1)*this.pageSize +","+this.pageSize;
        System.out.println("sql="+sql);

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章