oracle 1=1 1=2 区别

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 plain copy print ?
  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 plain copy print ?
  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   

 

 

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值