程序员在编程过程中,经常会在代码中使用到“where 1=1”,这是为什么呢?
SQL注入
初次看到这种写法的同学肯定很纳闷,加不加where 1=1,查询不都一样吗?例如:
select * from customers;
与
select * from customers where 1=1;
查询出来的结果完全没有区别呀。
是的,上面的查询结果是没有区别,但是这并不是我们要添加它的目的。我们知道1=1表示true,即永真,在SQL注入时配合or运算符会得到意向不到的结果。
例如,当我们要删除客户名称为“张三”的记录,我们可以这样写:
delete from customers where name='张三'
这个时候如果在where语句后面加上 or 1=1会是什么后果?即:
delete from customers where na