码迷,mamicode.com
首页 > 编程语言 > 详细

聚合函数:sum,avg,max,min,count;模糊查询;排序

时间:2015-03-19 23:44:24      阅读:299      评论:0      收藏:0      [点我收藏+]

标签:

----聚合函数 --做计算 做统计 对null过滤:null是指不知道什么值,所以无法做计算
--sum(参数):统计和
--avg(参数):求平均值
--max(参数):最大值
--min(参数):最小值
--count(参数):获取满足条件的记录数
--1.获取学员总人数
select COUNT(Email) from Student
--2.获取最大的年龄值 年龄值越大,年龄越小 年龄值越小,年龄越大
select MAX(BornDate) from Student
select min(BornDate) from Student
--3.求科目ID为1的考虑平均分
select AVG(StudentResult) from Result
--获取学号为2 的学员的考试总分
select SUM(StudentResult) from Result where StudentNo=2

---说明:参数的要求
count:*,字段,常量值,会过滤掉null
max..min:可以对任意类型的数据进行聚合,如果是非数值,那么就会按 数字字母等 的排序进行聚合
select MAX(Email) from Student where Email is not null
select min(StudentName) from Student
--sum..avg只能对数值类型进行聚合,对日期值都不可以
select AVG(ExamDate) from Result
select AVG(StudentName) from Student

--带条件的查询--where
--查询年龄在1990---1998之间的学员信息
select * from Student where BornDate>=‘1990-12-31‘ and BornDate<=‘1997-1-1‘
--数值或者日期范围也可以使用between and 相当于 >= <= 做了内部优化。效率更高
select * from Student where BornDate between ‘1990-12-31‘ and ‘1997-1-1‘

--查询班级ID为 1,2,3,4,5的学员信息
select * from Student where ClassId =1 or ClassId=2 or ClassId=3 or ClassId=4 or ClassId=5
--指定一个具体的范围可以使用in in里面包含的数值类型需要一致
select * from Student where ClassId in (1,2,3,4,‘a‘)
--查询班级id不是12345的学员信息
select * from Student where ClassId <>1 and ClassId<>2 and ClassId<>3 and ClassId<>4 and ClassId<>5
select * from Student where ClassId not in (1,2,3,4,‘5‘)

--模糊查询
--查询 “张” 姓 学员 =是严格的内容匹配
select * from Student where StudentName=‘张‘
--通配符:
--%:.* *:代表任意个任意字符
--_:代表任意一个字符
--[]:可以代表一个指定范围或者值的字符 [0-9a-zA-Z] [13579]
--[^]:取范围之外的一个值
select * from Student where StudentName=‘张%‘
--通配符必须在模糊查询中才是有特殊意义的通配符 必须使用关键字:like:像。。。。一样
select * from Student where StudentName like ‘%张%‘
select * from Student where StudentName like ‘张__‘
--查询学号在11~15号之间的学员信息
select * from Student where StudentNo like ‘1[1-5]‘
select * from Student where StudentName not like ‘张%‘
select * from Student where StudentName like ‘%文%‘

--null是指不知道什么值,所以对null不能做计算 判断的时候使用is null is not null
--ISNULL()函数的使用:如果是null就使用自定义的字符串进行显示的替换
select StudentName,ISNULL(Email,‘没有填写电子邮件‘) from Student where ClassId=2


--数据排序:只是对查询出来的结果集做记录的重排
--select 字段列表/* from 表列表 where 条件 Order by 排序字段列表 默认是升序 asc 降序:desc
select * from Student order by sex asc ,StudentNo desc

--1.查询六期班所有姓 陈 的学员-student
select ClassId from Classes where classname=‘七期班‘
select * from Student where StudentName like ‘陈%‘ and ClassId=5
--高级查询---子查询
select * from Student where StudentName like ‘张%‘ and ClassId=(select ClassId from Classes where classname=‘七期班‘)
--2.查询所有科目中包含c 字符的科目信息subject
select * from Subject where SubjectName like ‘%c%‘
--3.查询office最近一次考试时间 result
select MAX(ExamDate) from Result where SubjectId=(select SubjectId from Subject where SubjectName=‘office‘)
select top 1 ExamDate from Result where SubjectId=(select SubjectId from Subject where SubjectName=‘office‘) order by ExamDate desc

聚合函数:sum,avg,max,min,count;模糊查询;排序

标签:

原文地址:http://www.cnblogs.com/dianshen520/p/4351939.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!