http://blog.csdn.net/dingwb/archive/2007/06/15/1653402.aspx
where 1=1有什么用?在SQL语言中,写这么一句话就跟没写一样。
select * from table1 where 1=1与select * from
table1完全没有区别,甚至还有其他许多写法,1<>2,'a'='a','a'<>'b',其目的就只有一个,where
的条件为永真,得到的结果就是未加约束条件的。
在SQL注入时会用到这个,例如select * from table1 where name='lala'给强行加上select * from table1 where name='lala' or 1=1这就又变成了无约束的查询了。
最近发现的妙用在于,在不定数量查询条件情况下,1=1可以很方便的规范语句。例如一个查询可能有name,age,height,weight约束,也可能没有,那该如何处理呢?
String sql=select * from table1 where 1=1
为什么要写多余的1=1?马上就知道了。
if(!name.equals("")){
sql=sql+"name='"+name+"'";
}
if(!age.equals("")){
sql=sql+"age'"+age+"'";
}
if(!height.equals("")){
sql=sql+"height='"+height+"'";
}
if(!weight.equals("")){
sql=sql+"weight='"+weight+"'";
}
如果不写1=1呢,那么在每一个不为空的查询条件面前,都必须判断有没有where字句,否则要在第一个出现的地方加where
where 1=1的写法是为了检化程序中对条件的检测
打个比方有三个参数a, b, c
@sql=select * from tb'
这三个参数都可能为空
这时你要构造语句的话,一个个检测再写语句就麻烦
比如
if @a is not null
@sql=@sql + " where a=' + @a
if @b is not null
这里你怎么写?要不要加where 或直接用 and ?,你这里还要对@a是否为空进行检测
用上 where 1=1 之后,就不存在这样的问题, 条件是 and 就直接and ,是or就直接接 or
----------------------------------count(1) 与 count(*)
这个要看你自己数据库设计的结构啦
如果你的数据表没有主键,那么count(1)比count(*)快
如果有主键的话,那主键(联合主键)作为count的条件也比count(*)要快
如果你的表只有一个字段的话那count(*)就是最快的啦
count(*) count(1) 两者比较。主要还是要count(1)所相对应的数据字段。
如果count(1)是聚索引,id,那肯定是count(1)快。但是差的很小的。
因为count(*),自动会优化指定到那一个字段。所以没必要去count(?),用count(*),sql会帮你完成优化的
分享到:
相关推荐
三、技巧 1、1=1,1=2的使用,在SQL语句组合时用的较多 “where 1=1” 是表示选择全部 “where 1=2”全部不选, ...set @strSQL = 'select count(*) as Total from [' + @tblName + '] where 1=1 安定 '+ @strWhere
9、视图是一种常用的数据对象,它是提供(1___)和(1___)数据的另一种途径,可以简化数据库操作,当使用多个数据表来建立视图时,表的连接不能使用(2___)方式,并且不答应在该语句中包括(3___)等要害字。...
查询自己下级生成的订单数量最多的员工.(注:每个下级员工的订单数和最多) select employeeid from Employees where employeeid in( select employeeid from orders group by employeeid having count(orderid)>=all...
ssion count" from v$session where status='ACTIVE ******** total sessions count *************** sion count" from v$session; ******** top 30 big objects name ********** a35 a15 -----部分截断sql---
set @strSQL = 'select count(*) as Total from [' + @tblName + '] where 1=1 安定 '+ @strWhere 2、收缩数据库 --重建索引 DBCC REINDEX DBCC INDEXDEFRAG --收缩数据和日志 。。。。。。。。。。。。。。。。等等...
jdbcUrl=jdbc:oracle:thin:@193.0.10.110:1521:MG01 username=DBUSROMS1 password=**** count=1 数据库连接 DELETE FROM SELLMAIL WHERE SMKEY = :B1 当前正执行SQL jdbcUrl=jdbc:oracle:thin:@193.0.10.110:1521:...
代码如下:SELECT SUM(正确数)+SUM(错误数) AS 总记录数,SUM(正确数),SUM(错误数) FROM ( SELECT COUNT(1) 正确数,0 错误数 FROM TB WHERE STATUS=1 UNION ALL SELECT 0 正确数,COUNT(1) 错误数 FROM TB WHERE ...
设表名为X,字段为C1,C2,C3,.... 做一下查询: select count(*),c1,c2,c3,.... from X group by c1,c2,c3.... having count(*)>1 ... WHERE T1.COL1 = T2.COL1 AND T1.COL2 = T2.COL2)>1 GROUP BY COL1,COL2
学生成绩管理系统 ...strcmd = "select count(*) from teacher where 教师编号='" + txtUsername.Text + "' and 密码='" + txtPassword.Text + "'"; temp = 2; } if (DropDownList1.Text == "学生") {
前言 本文主要给大家介绍了关于MySQL...SELECT * FROM t_info a WHERE ((SELECT COUNT(*) FROM t_info WHERE Title = a.Title) > 1) ORDER BY Title DESC 一、查找重复记录 1、查找全部重复记录 SELECT * FROM t_inf
[debug] http://www.rayner.com/products.php?id=22/**/AND/**/1=2/**/UNION/**/SELECT/**/1,concat(0x1e,0x1e,COUNT(*),0x1e,0x20),3,4,5,6,7,8,9 ,10/**/FROM/**/db2889_rayner_en.auth/**/WHERE/**/pass=0x...
4. sql_count = “select count(*) from article a where 1=1 “; //这条语句中a就代表article 这张表,后面可以写a.字段来代表该表的字段,where 1 = 1,就相当于提供了一个where。因为1=1永远成立。 就可以根据...
insert into ntable select sname,sdept from student where sno in (select sno from sc where grade<60 group by sno having count(grade)>=3); 总结: 1.通过这次实验我学会了SQL的常用数据更新操作。 2.能熟练...
if li_count < 1 then messagebox("提示信息","请输入正确的用户名!") sle_user.setfocus() return end if //查询密码以及一些别的有用数据 select 密码 into :ls_passdata from 用户清单 where 姓名 =:ls_user;...
select *, (select count(*) from student where cid = classes.id) as num from classes order by num; # in, not in子句查询示例 查询班级id大于小于的这些班级的学生信息 select * from student where cid ...
基于.NET网上书店开发系统 Public partial class register : System.Web.UI.Page { protected void Page_Load(object ... string sql1 = "select count(*) from tbluser where userName='" + TextBox1.Text + "'";
SELECT sno,sname FROM Student WHERE gender='男' 2、使用SELECT语句进行条件查询 (1)在grade表中查找分数在80~90范围内的学生的学号和分数。 SELECT sno,score FROM SC WHERE score>=80 and score(2)在grade...
String countSql = "select count(*) from userinfo where 1=1"+conditionSql; pager.setTotalCount(getUserCount(countSql)); return list; } //查询条数 public int getUserCount(String sql){ int ...
DocDs = GetDataSet("select MAX(CAST(SUBSTRING(NewsID,3,12) AS BIGINT)) as NewsID from News where (SUBSTRING(NewsID,3,8))='" + today + "'"); string id = DocDs.Tables[0].Rows[0]["NewsId"].ToString()...
法1:select * from sc where 课程号='3-105' and 成绩 between 60 and80; 法2:select * from sc where 课程号='3-105' and 成绩 > 60 and 成绩 ; 2.查询成绩为85、86或88的记录。 注释:用于制定某个集合使用 in ...