oracle 1=1 1=2 区别

来源:互联网 发布:阿里云杭州和北京对比 编辑:程序博客网 时间:2024/06/02 12:58

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 块动态变化的。

 

  1. String sql="select * from table_name  where 1=1";  
  2.         if( conditon 1) {  
  3.               sql=sql+"  and  var2=value2";  
  4.             }  
  5.         if(conditon 2) {  
  6.               sql=sql+"  and var3=value3";  
  7.            }   

  where 1=1 是为了避免where 关键字后面的第一个词直接就是“and”而导致语法错误。

·因为是一个永真条件,可以用于任何需要的地方。

[c-sharp] view plaincopyprint?
  1. SQL> create table t as select * from dept where 1=1;  
  2. SQL> select * from t;  
  3. DEPTNO DNAME          LOC  
  4. ------ -------------- -------------  
  5.     10 ACCOUNTING     NEW YORK  
  6.     20 RESEARCH       DALLAS  
  7.     30 SALES          CHICAGO  
  8.     40 OPERATIONS     BOSTON   

2.where 1=2:返回一个永假条件

其他写法:where 1>2; where 1=0等;

·建立表,与现有表结构相同,但是不需要向其填充任何数据

[c-sharp] view plaincopyprint?
  1. SQL> create table t1 as select * from dept where 1=0;  
  2. SQL> select * from t1;  
  3. DEPTNO DNAME          LOC  
  4. ------ -------------- -------------  
  5. SQL> desc t1;  
  6. Name   Type         Nullable Default Comments   
  7. ------ ------------ -------- ------- --------   
  8. DEPTNO NUMBER(2)    Y                           
  9. DNAME  VARCHAR2(14) Y                           
  10. LOC    VARCHAR2(13) Y