SQL SERVER-2-聚合函数演示|模糊查询和通配符|null值处理

1.聚合函数演示
--求平均值
--注意,如果字段tsage的数据类型为int,返回的结果也是整形,会被自动转换
select avg(tsage) as 平均年龄 from TblStudent
--如果要保留精度可以入下面的写法,或者数据类型改成decmial
select avg(tsage*1.0) as 平均年龄 from TblStudent

--为输出结果起个别名的方式如下三种
select avg(tsage*1.0) as 平均年龄 from TblStudent
select avg(tsage*1.0) 平均年龄 from TblStudent
select 平均年龄=avg(tsage*1.0) from TblStudent

--当使用聚合函数的时候,注意在select查询列表中不要出现除了使用聚合函数以外的其他列(不要直接写其他列名),
--除非该列也使用了聚合函数或者是该列包含在了Group By子句中
--如下的写法会报错
select avg(tsage*1.0) as 平均年龄,tsname from TblStudent
--可以写成下面的样式
select avg(tsage*1.0) as 平均年龄,tsname from TblStudent group by tsname


--求总和
--求总年龄
select sum(tsage) as 总年龄 from TblStudent
--求最大年龄(年龄最大的可能有多个人,但是只求出最大的值)
select max(tsage) as 最大值 from TblStudent
--求最小年龄
select min(tsage) as 最大值 from TblStudent
--求记录的总条数
select count(*) as 记录总条数 from TblStudent

--把id是偶数的全部设置成null
update TblStudent set tsage=null where tsid%2=0
--统计总条数,如果tsage的值是null,则null值会被忽略不参与统计
select count(tsage) as 记录总条数 from TblStudent

--avg,count,min,max等聚合函数在计算的时候都会对null值进行过滤

--下面的三种方式查询结果都是相同的,比较灵活
select * from
(select count(tsage) as 记录 from TblStudent) as a,
(select sum(tsage) as 记录 from TblStudent) as b
select max(tsage) as 记录 from TblStudent
select min(tsage) as 记录 from TblStudent
select avg(tsage*1.0) as 记录 from TblStudent

select
    count(tsage),
    sum(tsage),
    max(tsage),
    min(tsage),
    avg(tsage)
from TblStudent

select
    总条数=(select count(tsage) as 记录总条数 from TblStudent),
    总和=(select sum(tsage) as 记录总条数 from TblStudent),
    平均年龄=(select avg(tsage*1.0) as 记录总条数 from TblStudent)


2.准确查询
--查询英语没有及格的学生的学号
select tSId from TblScore where tEnglish<60

--查询年龄在20到30岁之间的男学生
select * from TblStudent where tsage>=20 and tsage<=30 and tsgender=N'男'
--上面的查询结果和下面的一样
select * from TblStudent where (tsage>=20 and tsage<=30) and tsgender=N'男'

--下面的结果为年龄在20-30之间的,或者是性别为男
select * from TblStudent where tsage>=20 and tsage<=30 or tsgender=N'男'
--统计结果和下面的一样
select * from TblStudent where (tsage>=20 and tsage<=30) or tsgender=N'男'

--下面的查询结果为年龄大于20 或者年龄小于30且性别为男
select * from TblStudent where tsage>=20 or tsage<=30 and tsgender=N'男'
--统计结果和下面的一样
select * from TblStudent where tsage>=20 or (tsage<=30 and tsgender=N'男')


--not and or 是3个逻辑运算符,优先级为not最高其次是and最后是or


--Between…and …在...之间
--查询年龄在20-30岁之间的男学生,他是包含边界值的
select * from TblStudent where tsage between 20 and 30

--查询math成绩在80到90分之间的所有学生的的ID
select tsid from tblscore where tmath between 65 and 70

--查询班级id为1,2,3的所有学生
select * from TblStudent where tsclassid=1 or tsclassid=2 or tsclassid=3
--也可以使用in,查询结果是相同的,但是这种写法效率比较低,如果可以做最好不要使用这种方式
select * from TblStudent where tsclassid in (1,2,3)
--如果上面的写法可以优化成下面这种写法,则尽量用下面这种写法
select * from TblStudent where tsclassid>=1 and tsclassid<=3


3.模糊查询和通配符

--第一个通配符:%,表示任意多个任意字符
--当使用通配符来匹配的时候必须使用like
--查询出来的是  张XXX
select * from TblStudent where tsname like N'张%'
--查询出来的是  XXX张XXX
select * from TblStudent where tsname like N'%张%'

--通过[]将%包含起来,则%不在表示一个通配符,而表示一个普通的字符
select * from TblStudent where tsname like '%[%]%'

--通配符:_ ,统配任意的单个字符,有且只有一个
select * from TblStudent where tsname like '貂_'
--效果相当于下面的查询方式
select * from TblStudent where tsname like '貂%' and len(tsname)=2

--通配符:[]
select * from TblStudent where tsname like '张[axc]妹'

select * from TblStudent where tsname like '张[0-9]妹'
select * from TblStudent where tsname like '张[a-z]妹'
select * from TblStudent where tsname like '张[A-Z]妹'


-- like 和 not like
--像这样一样   和不像这样
select * from TblStudent where tsname like '张惠妹'
select * from TblStudent where tsname like '张[^0-9]妹'
select * from TblStudent where tsname not like '张[0-9]妹'

4.空值处理
update TblStudent set tsname=null where tsid=1 --表示数据库中的空值
update tblStudent set tsname='' where tsid=2 --表示的是一个长度为0的字符串,表示一个字符串。


--请查询出所有tsage为null的学生的记录
--如下的两个查询语句都查询不出数据
--这是应为数据库中的null值比较特殊,表示一个unknow的值
--就是不知道,数据库中的值要么是真,要么是假,要么是不知道(unknow),所以如果条件等于或者不等于不知道(nuknow)的时候系统是无法识别的
select * from TblStudent where tsage=null
select * from TblStudent where tsage<>null

--在数据库中对null值不能用=或<>来判断,要判断null值,只能使用一个特殊的运算符 is
select * from TblStudent where tsage is null

--查询所有tsage不为null的数据的记录。
select * from TblStudent where tsage is not null

--isnull(),表示一个函数

--null值的运算
--让所有人的年龄增加5
--如果数据中存储的年龄是null,则结果还是null,null与任何值运算结果都是null
update TblStudent set tsage=tsage+5 where tsid=2

猜你喜欢

转载自blog.csdn.net/m0_37532448/article/details/82867456
今日推荐