String sql="select * from table_name where 1=1";
if( conditon 1) {
sql=sql+" and var2=value2";
}
if(conditon 2) {
sql=sql+" and var3=value3";
}
上述代码是为了实现动态的SQL查询语句,满足不同的查询情况。《where 1=1》 是为了避免where 关键字后面的第一个词直接就是 “and”而导致语法错误。where后面总要有语句,加上了1=1后就可以保证语法不会出错!
select * from table where 1=1
因为table中根本就没有名称为1的字段,所以该SQL等效于select * from table。
1.拷贝表
create table table_name as select * from Source_table where 1=1;
2.复制表结构
create table table_name as select * from Source_table where 1 <> 1;