zoukankan      html  css  js  c++  java
  • 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

  • 相关阅读:
    列表去重
    URL和URI的不同
    functional program language
    thinkphp5_笔记二
    关于《提问智慧》的笔记
    实习记录_2
    关于用户表的设计
    30秒运行超时的错误(Maximum execution time of 30 seconds exceeded)
    thinkphp5_笔记一
    Jquary 和Ajax实现简单的异步请求
  • 原文地址:https://www.cnblogs.com/xijin-wu/p/5590010.html
Copyright © 2011-2022 走看看