1=1 means true and 1=2 means false
where 1=2
create table t1 as select * from t
t1 will be same as t without data.
where 1=1
I have seen this clause many queries, i am not sure of the reason.
1.where 1=1:将返回一个true值,既条件为真。
还有其他的写法:where 0=0;where 'a'='a'等;
·常由程序(c++,java,c#等)生成,where条件中 1=1 之后的条件是通过 if 块动态变化的。
- 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”而导致语法错误。
·因为是一个永真条件,可以用于任何需要的地方。
- SQL> create table t as select * from dept where 1=1;
- SQL> select * from t;
- DEPTNO DNAME LOC
- ------ -------------- -------------
- 10 ACCOUNTING NEW YORK
- 20 RESEARCH DALLAS
- 30 SALES CHICAGO
- 40 OPERATIONS BOSTON
2.where 1=2:返回一个永假条件
其他写法:where 1>2; where 1=0等;
·建立表,与现有表结构相同,但是不需要向其填充任何数据
- SQL> create table t1 as select * from dept where 1=0;
- SQL> select * from t1;
- DEPTNO DNAME LOC
- ------ -------------- -------------
- SQL> desc t1;
- Name Type Nullable Default Comments
- ------ ------------ -------- ------- --------
- DEPTNO NUMBER(2) Y
- DNAME VARCHAR2(14) Y
- LOC VARCHAR2(13) Y