sql查询所有数据并把中间数据靠前

本文介绍了如何使用MySQL查询语句从数据库中获取所有记录,并确保中间部分的数据优先显示。这对于数据排序和分析有特殊需求的场景非常有用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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);

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值