mysql 多条件组合查询
注意:里面的单引号和双引号,这里的判断也可以用eques。
//基础的SQL语句
String sql = " select * from v_userinfo where 1=1 ";
//拼接查询条件---------------start-------------------------------------
//用户姓名
String realName = puser.getRealname();
if(realName != null && realName.length() > 0)
{
sql += " and realname like '%" + realName + "%' ";
}
//用户性别
String sex = puser.getSex();
if(sex != null && sex.length() > 0)
{
sql += " and sex= '" + sex + "' ";
}
//证件类型
int certTypeId = puser.getCertTypeID();
if(certTypeId > 0)
{
sql += " and cert_type= " + certTypeId ;
}
//证件号码
String cert = puser.getCert();
if(cert != null && cert.length() > 0)
{
sql += " and cert like '%" + cert + "%' ";
}
//证件类型
int userTypeID = puser.getUserTypeID();
if(userTypeID > 0)
{
sql += " and user_type= " + userTypeID ;
}
//拼接查询条件---------------end----------------------------
System.out.println("查询的SQL:" + sql );