SQL where 1=1的作用
2016-06-16 10:08
831 查看
浅谈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
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
相关文章推荐
- 3# CentOS 6.7 x64 Apache/PHP/Mariadb环境安装Redmine3.2.1
- hibernate 调用存储过程
- Oracle树查询及函数
- 经典SQL语句大全
- oracle分区表详解
- PreparedStatement 批量更新,插入数据到Oracle
- Mysql转存SQL后,重新运行SQL,第一行正确,第二行开始报乱码
- 删除 对于 数据库“Teacher”失败。
- MySQL设计规约
- Redis
- sql 变量
- sql 添加变量
- MYSQL的常用命令和增删改查语句和数据类型
- SQL Server 数据库定时自动备份
- SQL Server中变量的声明和使用方法
- sql 批量插入
- 数据库管理系统MFC实现
- MySQL日期数据类型、时间类型总结
- sql添加字段 2016.06.15回顾
- FineReport如何连接和使用MongoDB数据库