SQL中的 where 1= 1?以及select 1 from table?意义!
来源:互联网 发布:网络兼职骗局信誉代刷 编辑:程序博客网 时间:2024/05/01 08:51
SQL语句中的 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+"'";
这样方便很多,及时username,password两者都为空都可以查询
永远为真
主要是为了便于动态连接后续条件
有时候想查看一下表的字段就
select * from UB where 1=2,表示false 1=1表示true
-------------------------------------------
package StringTest;
public class Where {
/**
* 我用于高级查询情况下
* @param args
*/
public static void main(String[] args) {
Where s= new Where();
s.print("13432134321", "www.cjfuture.cn");
//null时,即为用户没有写值
s.print(null, "www.cjfuture.cn");
}
public void print(String mobile,String url){
StringBuffer sb = new StringBuffer("select id from tab where 1 = 1 ");
//如果没有1=1,下面的判断语句会比现在写复杂
if(mobile!=null){
sb.append("and mobile = "+mobile);
}
if(url!=null){
sb.append("and name = "+url);
}
System.out.println(sb.toString());
}
public void otherPrint(String mobile,String url){
StringBuffer sb = new StringBuffer("select id from tab");
//如果没有1=1,下面的判断语句会比现在写复杂,where放在哪里合适?
if(mobile!=null){
sb.append("and mobile = "+mobile);
}else{
sb.append(""); //如何写?
}
if(url!=null){
sb.append("and name = "+url);
}else{
sb.append(""); //如何写?
}
System.out.println(sb.toString());
}
}
table表是一个数据表,假设表的行数为10行。
1:select 1 from table 增加临时列,每行的列值是写在select后的数,这条sql语句中是1
2:select count(1) from table 管count(a)的a值如何变化,得出的值总是table表的行数
3:select sum(1) from table 计算临时列的和
在SQL SERVER中用 1 测试了一下,发现结果如下:
1:测试结果,得出一个行数和table表行数一样的临时列(暂且这么叫,我也不知道该叫什么),每行的列值是1;
2:得出一个数,该数是table表的行数;
3:得出一个数,该数是table表的行数;
然后我又用“2”测试,结果如下:
1:得出一个行数和table表行数一样的临时列,每行的列值是2;
2:得出一个数,该数是table表的行数;
3:得出一个数,该数是table表的行数×2的数
然后我又用更大的数测试:
1:得出一个行数和table表行数一样的临时列,每行的列值是我写在select后的数;
2:还是得出一个数,该数是table表的行数;
3:得出一个数,该数是table表的行数×写在select后的数
综上所述:第一种的写法是增加临时列,每行的列值是写在select后的数;第二种是不管count(a)的a值如何变化,得出的值总是table表的行数;第三种是计算临时列的和。
语句if not exists(select 1 from deleted d join inserted i on d.ID=i.ID
and d.col1=1 and i.col1=2)
if not exists 如果不存在
(..)这里该是验证更新,更新前的id等于更新后的id,并且更新前col的值是1,更新后的值是2
SQL语句中的 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+"'";
这样方便很多,及时username,password两者都为空都可以查询
永远为真
主要是为了便于动态连接后续条件
有时候想查看一下表的字段就
select * from UB where 1=2,表示false 1=1表示true
-------------------------------------------
package StringTest;
public class Where {
/**
* 我用于高级查询情况下
* @param args
*/
public static void main(String[] args) {
Where s= new Where();
s.print("13432134321", "www.cjfuture.cn");
//null时,即为用户没有写值
s.print(null, "www.cjfuture.cn");
}
public void print(String mobile,String url){
StringBuffer sb = new StringBuffer("select id from tab where 1 = 1 ");
//如果没有1=1,下面的判断语句会比现在写复杂
if(mobile!=null){
sb.append("and mobile = "+mobile);
}
if(url!=null){
sb.append("and name = "+url);
}
System.out.println(sb.toString());
}
public void otherPrint(String mobile,String url){
StringBuffer sb = new StringBuffer("select id from tab");
//如果没有1=1,下面的判断语句会比现在写复杂,where放在哪里合适?
if(mobile!=null){
sb.append("and mobile = "+mobile);
}else{
sb.append(""); //如何写?
}
if(url!=null){
sb.append("and name = "+url);
}else{
sb.append(""); //如何写?
}
System.out.println(sb.toString());
}
}
- SQL中的 where 1= 1?以及select 1 from table?意义!
- select * from table where 1=1
- select * from table where 1=1
- select * from table where 1=1
- select * from table where 1=1
- select from table where 1=1
- select * from table where 1=1
- sql中的“select from 1” 和“where 1=1”
- SELECT * FROM {0} WHERE 0 = 1 的意义
- select * from tablename where 1=0的意义
- select * from table where 1<>1
- select * from table where 1=2 的作用
- discuz7.2问题SQL:SELECT * FROM [Table]notelist WHERE closed='0' AND app1'-5' LIMIT 1
- SQL 另类查询 select 1 from table
- select * from book where 1=1
- select 1 from table
- select 1 from table
- select 1 from table
- android Fragments详解二:创建Fragment
- The current branch is not configured for pull No value for key branch.master.merge found in configur
- J2EE开发时的包命名规则,养成良好的开发习惯
- 大趋势(一):软件化 / Software Defined
- ffmpeg+ffserver 搭建流媒体服务器
- SQL中的 where 1= 1?以及select 1 from table?意义!
- 怎样判断机器的字节顺序是高字节在前还是低字节在前?
- 面试宝典之程序设计基本概念
- Objective-C 类和对象
- android 的injustdecodebounds
- oracle中的包头(Package)与包体(Package body)
- 解决:彩信,添加了有效联系人和编辑了主题,按返回键,提示信息存为草稿,但实际未储存
- Log4Net使用指南
- 在SQL PLUS中打印存储过程的结果