SQL条件中的where 1=1 和 where 1 <> 1
where 1=1 是为了避免where 关键字后面的第一个词直接就是 “and”而导致语法错误。
一般用在代码(java、python…)可能用到SQL拼接中不确定是否有其他where条件时,动态SQL中连接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”而导致语法错误。
因为table中根本就没有名称为1的字段,所以该SQL等效于select * from table。
--可能存在其他使用场景的例子
-- 拷贝表
create table table_name as select * from Source_table where 1=1;
--复制表结构
create table table_name as select * from Source_table where 1 <> 1;