where条件中 1=1 之后的条件是通过 if 块动态变化的。例如:
String sql="select * from table_name where 1=1";
if( conditon 1) {
sql=sql+" and var2=value2";
}
if(conditon 2) {
sql=sql+" and var3=value3";
}
where 1=1 是为了避免where 关键字后面的第一个词直接就是 “and”而导致语法错误。
String sql="select * from table_name where 1=1";
if( conditon 1) {
sql=sql+" and var2=value2";
}
if(conditon 2) {
sql=sql+" and var3=value3";
}
where 1=1 是为了避免where 关键字后面的第一个词直接就是 “and”而导致语法错误。
本文介绍了一种在SQL查询中使用动态Where子句的方法,通过初始设置where 1=1来避免语法错误,并利用if条件块根据不同的业务需求动态添加筛选条件。
1078

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



