SQL技巧------(转)
1,where 1=1
我们先来看看这个语句的结果:select * from table where 1=1,其中where 1=1,由于1=1永远是成立的,返回TRUE,条件为真;所以,这条语句,就相当于select * from table,返回查询表中的所有数据。
使用 where 1=1 的好处
1 string MySqlStr=”select * from table where 1=1 ”; 2 3 if(Age.Text.Lenght>0) { MySqlStr=MySqlStr+“and Age=“+“'Age.Text'“; } 4 5 if(Address.Text.Lenght>0) { MySqlStr=MySqlStr+“and Address=“+“'Address.Text'“; }
现在,也存在两种假设
①种假设
如果两个IF都成立,那么,语句变为:
MySqlStr=”select * from table where 1=1 and Age='18' and Address='云南省文山州广南县小波吗村'”,很明显,该语句是一条正确的语句,能够正确执行,如果数据库有记录,肯定会被查询到。
②种假设
如果两个IF都不成立,那么,语句变为:
MySqlStr=”select * from table where 1=1”,现在,我们来看这条语句,由于where 1=1 是为True的语句,因此,该条语句语法正确,能够被正确执行,它的作用相当于:MySqlStr=”select * from table”,即返回表中所有数据。

浙公网安备 33010602011771号