SQL中where 1=1 和 where1<>1

而对于开发者,如果检索条件不确定,即根据用户的画面输入追加检索条件时,这的“where 1=1”就非常有用。

往简单说,它可以简化程序,免去繁琐的判断,提高程序效率。往复杂说呢,可以减轻程序员的开发负担,最大限度的提高工作效率。

上例子:
①使用1=1
String sql =Select A.x,A.Y FROM table A Where 1=1;
StringBuffer sqlBuffer = new StringBuffer ;
sqlBuffer.append(sql);
如果画面有三个独立的可选输入项目l,m.n
if ( l != null ) {
sqlBuffer.append("and A.l = l");
}
if ( m != null ) {
sqlBuffer.append("and A.m = m");
}
if ( n != null ) {
sqlBuffer.append("and A.n= n");
}

这个是使用了“1=1”的用法,直接判断追加就行。


②不使用1=1,至少要有如下繁琐的判断
String sql =Select A.x,A.Y FROM table A ;
StringBuffer sqlBuffer = new StringBuffer ;
sqlBuffer.append(sql);
if (l != 空) {
sqlBuffer.append("where A.l = l");
} else {
if (m != 空) {
sqlBuffer.append("where A.m = m");
} else {
if (n != 空)
sqlBuffer.append("where A.n = n");
}
}
if (l != 空 and m != 空) {
sqlBuffer.append(" and A.m = m");
}else if (l != 空 and m != 空 and n != 空) {
sqlBuffer.append(" and A.n = n");
}

每一个项目的追加都要判断前面的检索条件是否存在,以确定是否要“where”。
有了上面的比较1=1的优势就比较明显了。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值