1.拷贝表结构
create table table_name as select * from
Source_table where 1 <> 1;
且select * from Source_table where 1=1;等同于 select * from Source_table;
另有说法待验证(出自:Oracle海量数据查询优化_weixin_33894992的博客-CSDN博客)
select col1,col2 into #t from t where 1=0
这类代码不会返回任何结果集,但是会消耗系统资源的,应改成这样: create table #t(...)
2.做SQL拼接时 为了满足语法规范
我们在写代码的过程中,为了保证语法规范的时候,也会使用到where 1=1。
我们先看下面这段Java代码:
String sql="select * from table_name where 1=1"; if( condition 1) {
sql=sql+" and var2=value2";
} if(condition 2) {
sql=sql+" and var3=value3";
}
如果我们不写1=1的话,当condition 1为真时,代码拼接后被执行的SQL

本文详细介绍了SQL中where 1=1的多种用途,包括拷贝表结构时避免返回数据、在SQL拼接中保证语法正确性,同时也讨论了其可能带来的全表扫描的性能问题和在防止SQL注入中的作用。此外,文章提到了PreparedStatement在防止SQL注入方面的优势。
最低0.47元/天 解锁文章
707

被折叠的 条评论
为什么被折叠?



