where 1=1的妙用
where 1=1 表示永远为真!主要是为了便于动态连接后续条件
在组合查询条件时候多用:
String sql="select * from user where 1=1 ";
if(username!=null) sql=sql+ " and username='"+username+"'";
if(password!=null) sql=sql+ " and password='"+password+"'";
这样方便很多,及时username,password两者都为空都可以查询
写程序时也会这么用,比如说:
select *from table where @IsNull(field1,strPrint("filed1=$",0))
如果field1等于空的,那where 后面就没有条件了,势必会报错,所以加上1=1

浙公网安备 33010602011771号