请高手优化一个查询功能!非常感谢!

binbo2008 2010-10-12 05:15:10
我的经验不是太多,写的代码不优化,希望高手帮我优化一下查询,我先谢谢各位高手了。
下面是代码
ddh=request.getParameter("ddh");
date1=request.getParameter("date1");
date2=request.getParameter("date2");
shr=request.getParameter("shr");
zje1=request.getParameter("zje1");
zje2=request.getParameter("zje2");
paytype=request.getParameter("paytype");
posttype=request.getParameter("posttype");
if(date1==""&&date2==""&&zje1==""&&zje2==""){
byte aa[] = shr.getBytes("ISO-8859-1");
shr = new String(aa);
sql = "select * from forder_vi where djh like '%"+ddh+"%' and truename like '%"+shr+"%' and payid like '%"+paytype+"%' and pid like '%"+posttype+"%'";
}
else if(date1!=""&&date2!=""&&zje1==""&&zje2==""){
byte aa[] = shr.getBytes("ISO-8859-1");
shr = new String(aa);
sql = "select * from forder_vi where djh like '%"+ddh+"%' and truename like '%"+shr+"%' and payid like '%"+paytype+"%' and pid like '%"+posttype+"%' and otime between '"+date1+"' and '"+date2+"'";
}
else if(date1==""&&date2!=""&&zje1==""&&zje2==""){
byte aa[] = shr.getBytes("ISO-8859-1");
shr = new String(aa);
sql = "select * from forder_vi where djh like '%"+ddh+"%' and truename like '%"+shr+"%' and payid like '%"+paytype+"%' and pid like '%"+posttype+"%' and datediff(d,otime, '"+date2+"')>0";
}
else if(date1!=""&&date2==""&&zje1==""&&zje2==""){
byte aa[] = shr.getBytes("ISO-8859-1");
shr = new String(aa);
sql = "select * from forder_vi where djh like '%"+ddh+"%' and truename like '%"+shr+"%' and payid like '%"+paytype+"%' and pid like '%"+posttype+"%' and datediff(d,otime, '"+date1+"')<0";
}
else if(date1==""&&date2==""&&zje1!=""&&zje2!=""){
byte aa[] = shr.getBytes("ISO-8859-1");
shr = new String(aa);
sql = "select * from forder_vi where zje>"+zje1+" and zje<"+zje2+" and djh like '%"+ddh+"%' and truename like '%"+shr+"%' and payid like '%"+paytype+"%' and pid like '%"+posttype+"%'";
}
else if(date1==""&&date2==""&&zje1==""&&zje2!=""){
byte aa[] = shr.getBytes("ISO-8859-1");
shr = new String(aa);
sql = "select * from forder_vi where zje<"+zje2+" and djh like '%"+ddh+"%' and truename like '%"+shr+"%' and payid like '%"+paytype+"%' and pid like '%"+posttype+"%'";
}
else if(date1==""&&date2==""&&zje1!=""&&zje2==""){
byte aa[] = shr.getBytes("ISO-8859-1");
shr = new String(aa);
sql = "select * from forder_vi where zje>"+zje1+" and djh like '%"+ddh+"%' and truename like '%"+shr+"%' and payid like '%"+paytype+"%' and pid like '%"+posttype+"%'";
}
else if(date1!=""&&date2!=""&&zje1!=""&&zje2!=""){
byte aa[] = shr.getBytes("ISO-8859-1");
shr = new String(aa);
sql = "select * from forder_vi where zje>"+zje1+" and zje<"+zje2+" and djh like '%"+ddh+"%' and truename like '%"+shr+"%' and payid like '%"+paytype+"%' and pid like '%"+posttype+"%' and otime between '"+date1+"' and '"+date2+"'";
}
...全文
73 点赞 收藏 7
写回复
7 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
binbo2008 2010-10-12
if(shr!=""||shr!=null){
byte aa[] = shr.getBytes("ISO-8859-1");
shr = new String(aa);
if(date1==""&&date2==""&&zje1==""&&zje2==""){
sql = "select * from forder_vi where djh like '%"+ddh+"%' and truename like '%"+shr+"%' and payid like '%"+paytype+"%' and pid like '%"+posttype+"%'";
}
else if(date1!=""&&date2!=""&&zje1==""&&zje2==""){
sql = "select * from forder_vi where djh like '%"+ddh+"%' and truename like '%"+shr+"%' and payid like '%"+paytype+"%' and pid like '%"+posttype+"%' and otime between '"+date1+"' and '"+date2+"'";
}
else if(date1==""&&date2!=""&&zje1==""&&zje2==""){
sql = "select * from forder_vi where djh like '%"+ddh+"%' and truename like '%"+shr+"%' and payid like '%"+paytype+"%' and pid like '%"+posttype+"%' and datediff(d,otime, '"+date2+"')>0";
}
else if(date1!=""&&date2==""&&zje1==""&&zje2==""){
sql = "select * from forder_vi where djh like '%"+ddh+"%' and truename like '%"+shr+"%' and payid like '%"+paytype+"%' and pid like '%"+posttype+"%' and datediff(d,otime, '"+date1+"')<0";
}
else if(date1==""&&date2==""&&zje1!=""&&zje2!=""){
sql = "select * from forder_vi where zje>"+zje1+" and zje<"+zje2+" and djh like '%"+ddh+"%' and truename like '%"+shr+"%' and payid like '%"+paytype+"%' and pid like '%"+posttype+"%'";
}
else if(date1==""&&date2==""&&zje1==""&&zje2!=""){
sql = "select * from forder_vi where zje<"+zje2+" and djh like '%"+ddh+"%' and truename like '%"+shr+"%' and payid like '%"+paytype+"%' and pid like '%"+posttype+"%'";
}
else if(date1==""&&date2==""&&zje1!=""&&zje2==""){
sql = "select * from forder_vi where zje>"+zje1+" and djh like '%"+ddh+"%' and truename like '%"+shr+"%' and payid like '%"+paytype+"%' and pid like '%"+posttype+"%'";
}
else if(date1!=""&&date2!=""&&zje1!=""&&zje2!=""){
sql = "select * from forder_vi where zje>"+zje1+" and zje<"+zje2+" and djh like '%"+ddh+"%' and truename like '%"+shr+"%' and payid like '%"+paytype+"%' and pid like '%"+posttype+"%' and otime between '"+date1+"' and '"+date2+"'";
}
}
回复
binbo2008 2010-10-12
[Quote=引用 4 楼 cbxhpy 的回复:]

没有看错的话这一段是一样的:
byte aa[] = shr.getBytes("ISO-8859-1");
shr = new String(aa);

如果没有看出的话,sql中表是一样的,而且部分条件也是一样.

可以尝试一下把它们提出来放在前面的。需要新条件在sql+=“and 字段=%值%”;
[/Quote]

这个方法不错,我改一下 不知道运行效率有多大差别!
回复
binbo2008 2010-10-12
是的 写一个判断就可以了 我没写
回复
cbxhpy 2010-10-12
没有看错的话这一段是一样的:
byte aa[] = shr.getBytes("ISO-8859-1");
shr = new String(aa);


如果没有看出的话,sql中表是一样的,而且部分条件也是一样.

可以尝试一下把它们提出来放在前面的。需要新条件在sql+=“and 字段=%值%”;
回复
annabbj 2010-10-12
http://www.phome.asia/forum/thread/22693.html
java编程思想
回复
annabbj 2010-10-12
代码应该没有什么问题的,建议你下本java编程思想方面的书看看,这个我看了还不错。http://www.phome.asia/forum/thread/22695.html
回复
binbo2008 2010-10-12
代码运行是没有任何问题的!
回复
相关推荐
发帖
Web 开发
创建于2007-09-28

8.0w+

社区成员

Java Web 开发
申请成为版主
帖子事件
创建了帖子
2010-10-12 05:15
社区公告
暂无公告