已知:Mysql 表名 table 字段 a b c d
原来写法 select * from table where a = 1 and b = 2 and c = 3 and d =4;
但是a b c d字段可能给的缺省
前段只给了 a = 1;b =2;c= '' ;d = 4;
我还想这么写 select * from table where a = ? and b = ? and c = ? and d =?;
但是让c=?不判定,怎么办 select * from table where a = ? and b = ? and d =?;
我现在这么做:
String a= request.getParameter("a");
String b= request.getParameter("b");
String b= request.getParameter("c");
String d= request.getParameter("d");
String sql = "select * from table where 1=1 ";
if(!"".equals(a)){
sql += "and a = ?"
}
if(!"".equals(b)){
sql += "and b = ?"
}
if(!"".equals(c)){
sql += "and c = ?"
}
if(!"".equals(d)){
sql += "and d = ?"
}
感觉有点蠢,有好的mysql 语句或者什么办法吗?
我现在用的spring的jdbctemplate
项目没那么大,mybaits感觉用不上