首页 > 其他 > 详细

where 1=1

时间:2015-08-07 19:17:03      阅读:201      评论:0      收藏:0      [点我收藏+]

sql:

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

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

拷贝表 
create   table_name   as   select   *   from   Source_table   where   1=1;

复制表结构 
create   table_name   as   select   *   from   Source_table   where   1 <> 1;


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

例如:

Stringbuffer sql="select * from a where 1=1

if(type!= null){

  sql.append("and type="+type")

}

if(name != null){

  sql.append(" and name="+name);

}

如果没有 where 1= 1;

Stringbuffer sql="select * from a

if(type!= null){

  sql.append("where type="+type");

}else{

 

}

if(name != null){

  sql.append(" and name="+name);

}

就需要很复杂的判断查询条件语句为不为空,处理where 这词。

 

where 1=1

原文:http://www.cnblogs.com/huangcongcong/p/4711275.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!