关于多条件查询的问题
我用JSP做的一个根据三种条件查询的页面,我想的是判断其中三朝个表单,只要其中一个不为空就执行下面else 中的SQL语句,但是好象不对,高手们帮我分析分析,或者是多条件查询有更好的办法吗?
代码如下:
String key,keyy,keyyy;
key=request.getParameter( "key ");keyy=request.getParameter( "keyy ");keyyy=request.getParameter( "keyyy "); if((request.getParameter( "key ")==null || request.getParameter( "key ").trim().equals( " ")) && (request.getParameter( "keyy ")==null || request.getParameter( "keyy ").trim().equals( " ")) && ( request.getParameter( "keyyy ")==null || request.getParameter( "keyyy ").trim().equals( " ")))
sql= "select * from sms_LH_content order by SENDTIME desc ";
else
{
sql= "select * from sms_LH_content where (to_char(sendtime, 'yyyy-mm-dd ')=( ' "+key+ " ')) or (FENLEI= ' "+keyy+ " ') or (CONTENT like '% "+keyyy+ "% ') order by SENDTIME desc ";
}
------解决方案--------------------那就比较麻烦了,试试:
String key = request.getParameter( "key ")==null? " ":request.getParameter( "key ");
String keyy = request.getParameter( "keyy ")==null? " ":request.getParameter( "keyy ");
String keyyy = request.getParameter( "keyyy ")==null? " ":request.getParameter( "keyyy ");
String sql= "select * from sms_LH_content ";
if(key!=null && !key.equals( " "))
{
sql+= " where to_char(sendtime, 'yyyy-mm-dd '= ' "+key+ " ') ";
if(keyy!=null && !keyy.equals( " "))
{
sql+= " or FENLEI= ' "+keyy+ " ' ";
}
if(keyyy!=null && !keyyy.equals( " "))
{
sql+= " or CONTENT like '% "+keyyy+ "% ' ";
}
}
else if(keyy!=null && !keyy.equals( " "))
{
sql+= " where FENLEI= ' "+keyy+ " ' ";
if(keyyy!=null && !keyyy.equals( " "))
{
sql+= " or CONTENT like '% "+keyyy+ "% ' ";
}
}
else if(keyyy!=null && !keyyy.equals( " "))
{
sql+= " where CONTENT like '% "+keyyy+ "% ' ";
}
sql+= " order by SENDTIME desc ";
------解决方案--------------------其实楼主的代码和ls的差不多,可行