oracle中的 where 1=1 和where 1<>1

来源:互联网 发布:mysql的事务是什么 编辑:程序博客网 时间:2024/04/29 11:43

转载自:http://blog.csdn.net/guorun18/article/details/49802245

1=1 永真, 1<>1 永假。

1<>1 的用处: 用于只取结构不取数据的场合
例如:

create table table_temp tablespace tbs_temp as select * from table_ori where 1<>1 

建成一个与table_ori 结构相同的表table_temp,但是不要table_ori 里的数据。(除了表结构,其它结构也同理)

1=1的用处: 用于动态SQL

在组合查询条件时候多用:

String sql="select * from user where 1=1 "; if(username!=null) sql=sql+ " and username='"+username+"'"; if(password!=null) sql=sql+ " and password='"+password+"'"; 

这样方便很多,及时username,password两者都为空都可以查询

永远为真 相当于没有限制名称条件。但是如果没有1=1的条件,

select * from user where 会报错