SQL where 1=1的作用

浅谈where 1=1

1、简单理解的话where 1=1 永真, where 1<>1 永假

2、1<>1 的用处:
    用于只取结构不取数据的场合
    例如:
    create table table_temp tablespace tbs_temp as
    select * from table_ori where 1<>1
    建成一个与table_ori 结构相同的表table_temp,但是不要table_ori 里的数据。(除了表结构,其它结构也同理)

3、1=1的用处
     用于动态SQL
     例如 lv_string := 'select tbl_name,tbl_desc from tbl_test where 1=1 '||l_condition;
     当用户选择了查询的名称'abc'时l_condition :='and tbl_name = ''abc'''';但是当用户没有

    选择名称查询时l_condition就为空 这样 lv_string = 'select tbl_name,tbl_desc from tbl_test

    where 1=1 ' ,运行也不会出错,相当于没有限制名称条件。但是如果没有1=1的条件,则lv_string =

   'select tbl_name,tbl_desc from tbl_test where ';这样就会报错。

4、where 1=1就是条件永远为真,查出所有数据来 在组合查询条件时候多用,主要是为了便于动态连接后续条件

     String sql="select * from user where 1=1 ";

     if(username!=null){

      sql=sql+ " and username='"+username+"'";

     }

     if(password!=null){

      sql=sql+ " and password='"+password+"'";

      }


  转:http://blog.csdn.net/fanyuna/article/details/5972437

原文地址:https://www.cnblogs.com/xijin-wu/p/5590010.html