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 );
本文详细介绍了如何在MySQL中使用动态SQL语句进行多条件组合查询。通过拼接字符串的方式,根据不同的业务需求灵活地添加查询条件,如用户姓名、性别、证件类型等,实现了高效且灵活的数据检索。
1498

被折叠的 条评论
为什么被折叠?



