您的位置:首页 > 数据库

数据库复习总结(8)-查询(重要)连接、聚合函数、开窗函数

2017-12-07 21:01 441 查看

1.--------------------别名、查询全部列、部分列
    -》为表起别名as
    -》查询全部列、指定列
      为列起别名as

select * from UserInfo
select * from UserInfo as ui        --查询表中所有的列   (as作用起别名,其实最好不要起别名)
select  UserName  ,UserPwd  from UserInfo
select  UserName as name ,UserPwd as pwd from UserInfo as ui  --查询表中的部分列
View Code

2. -----------------查询前n部分数据:

top n 列名:表示查看前n行
top n percent 列名:表示查看前百分之几的数据

top一般都与order by连用

-》排序:order by 列名1 asc|desc,列名1 asc|desc...

select top 1 * from StudentInfo

select top 2 percent * from StudentInfo

select top 2 percent * from StudentInfo order by sId desc

select  * from StudentInfo order by sId desc,cid asc

3.------------------------消除重复行:distinct

--消除重复行(比如消除cid重复的)
select distinct cid from StudentInfo

4.---------------------条件查询:写在where后面

(1) 比较运算符

对行进行筛选,返回bool类型的值,如果某行中的列数据满足条件,则加入结果集,否则不出现在结果集中

          比较运算符:=,>,>=,<,<=,!=或<> (注意,这里的等于号就相当于C#当中的==号)

--where
select * from StudentInfo where sId=1
select sname  from StudentInfo where sId=1
select sname from StudentInfo where sId>5

 (2) between ... and ...表示在一个连续的范围内

--取出编号从3-8的学生
select * from StudentInfo where sId between 3 and 8
--取考试分数在60-80间的成绩
select * from ScoreInfo where scoreValue between 60 and 80 

--取班级在1-3间的学生信息
(解析:先对表StudentInfo操作 ,然后在对列cid操作,最后对行操作)
 select * from ScoreInfo where scoreValue between 1 and 3

(3)in表示在一个非连续的范围内

          逻辑运算符:and,or,not

--取班级编号为1-3的学生信息
select * from StudentInfo where cid between 1 and 3

select * from StudentInfo where cid>=1 and cid<=3

--取班级编号为1或3或8或10或100的学生信息

select * from StudentInfo where cid not in (1,3)

select * from StudentInfo where cid=1 or cid=3

--学生编号在[3,8],在1班,学生信息
select * from StudentInfo where (sId between 3 and 8) and (cid=1)

select * from StudentInfo where sGender=1

(4)模糊查询:用于处理字符串类型的值,运算符包括:

                           (1)关键字: like

                           (2)元字符:  % 、_ 、[]、 ^

%:0到多个任意字符。

_:1个任意字符

[]:1个某范围内的字符

^:^写在[]内部的开头,表示不使用内部的任何字符
                       

     %与_写在[]中表示本身的含义
    在[]表示一个连续的范围可以使用-                      

    null的判断:使用is null或is not null,与其它值计算时返回null,排序时null被认为是最      

    优先级:小括号,not,比较运算符,逻辑运算符

 

--模糊查询
--名字中包含三的学生信息
select * from StudentInfo where sName like '%三%'
--姓张的学生
select * from StudentInfo where sName like '张%'
--姓名为2个字的姓黄的学生
select * from StudentInfo where sName like '黄_'
--查询使用电话为13段的学生信息
select * from StudentInfo where sPhone like '13%'

select * from StudentInfo where sPhone like '1[^579]%'

--取使用qq邮箱的学生
select * from StudentInfo where sEMail like '%@qq%'
update StudentInfo set sPhone =null  where sId in(5,6)

--电话为nul的学生信息
select * from StudentInfo where sPhone is not null

(5)连接查询:join 表名 on 关联条件

--连接查询:

应用背景:当需要的结果从多张表中取时
如:--查询学生姓名及所在班级名称
--StudentInfo
--ClassInfo
--关系:StudentInfo.cid=>ClassInfo.cid
--关键问题:哪些表,关系

语法:

-》连接:join 表名 on 关联条件
内连接:inner join,两表中完全匹配的数据
左外连接:left outer join,两表中完全匹配的数据,左表中特有的数据
右外连接:right outer join,两表中完全匹配的数据,右表中特有的数据
完全外连接:full outer join,两表中完全匹配的数据,左表中特有的数据,右表中特有的数据

如图:

ClassInfo表

StudentInfo表:

内连接:inner join,两表中完全匹配的数据

例1:查询学生表和班级表:

select * 
from StudentInfo as si inner join ClassInfo as ci on si.cid=ci.cid

如图:

 改:只要学生姓名和班级姓名。

select si.sName,ci.cTitle
from StudentInfo as si
inner join ClassInfo as ci on si.cid=ci.cid

改2:查询青龙班所有的学生姓名:

select si.sName,ci.cTitle
from StudentInfo as si
inner join ClassInfo as ci on si.cid=ci.cid
where ci.cTitle='青龙'

左外连接:left outer join,两表中完全匹配的数据+左表中特有的数据

select *
from ClassInfo as ci
left join StudentInfo as si on ci.cId=si.cid

右外连接:right outer join,两表中完全匹配的数据+右表中特有的数据

select *
from  StudentInfo as ci
right join  ClassInfo as si on ci.cId=si.cid

select *
from  ClassInfo  as ci
right join   StudentInfo as si on ci.cId=si.cid-------注意此时右表中没有特殊的数据

完全外连接:full outer join,两表中完全匹配的数据+左表中特有的数据+右表中特有的数据

select *
from ClassInfo as ci
full join StudentInfo as si on ci.cId=si.cid

例题:查询

--班级名称、学生姓名、科目名称、分数

分析:
--StudentInfo,SubjectInfo,ScoreInfo,classinfo
--sid=stuid,sid=subid,

select class.cTitle,stu.sName,sub.sTitle,score.scoreValue
from ScoreInfo as score
inner join StudentInfo as stu on score.stuId=stu.sId
inner join SubjectInfo as sub on score.subId=sub.sId
inner join ClassInfo as class on stu.cid=class.cId
where class.cId=1

(6)聚合函数:对行数据进行合并

-》聚合函数:

 作用:对行数据进行合并

 函数: sum,avg,count,max,min
  注意:(1) 一般是对数字类型的列进行操作
             (2)一条查询中可以同时写多个聚合函数,但是不能与普通列混写
             (3)聚合中的null问题:不参与计算

例子:查询一班有多少人?

select COUNT(*) as count1 from StudentInfo where cid=1

例子:求科id=2的得分最大值

select * from ScoreInfo--------先查询一下表

select MAX(scorevalue) from ScoreInfo
where subId=2

--求学生编号为1的最低分
select min(scorevalue) from ScoreInfo
where stuId=1


--求语文科目的平均分 --SubjectInfo,ScoreInfo,avg select AVG(scoreValue) from SubjectInfo inner join ScoreInfo on subId=SubjectInfo.sId where sTitle='语文'

(7)开窗函数:over()

-》开窗函数:over()
    作用:

将统计出来的数据分布到原表的每一行中
结合聚合函数、排名函数使用

--将统计信息分布到行中
select ScoreInfo.*,avg(scorevalue) over()
from ScoreInfo
where subId=1

(8)分组:group by 列名1,列名2...

-》分组:group by 列名1,列名2...
聚合函数一般结合分组使用,进行分组内的数据进行统计
根据指定列进行分组
分组后条件筛选:having ...

--统计男女生人数
select sGender,COUNT(*)
from StudentInfo
group by sGender

--求每个班的男女生人数
--结果集中,分组依据值相同的实例在一组,
--在结果列中只能出现分组依据列和聚列
select sGender,cid,COUNT(*)
from StudentInfo
group by sGender,cid

--统计学生编号大于2的各班级的各性别的学生人数
select cid,sGender,COUNT(*)
from StudentInfo
where sId>2
group by cid,sGender

--统计学生编号大于2的各班级的各性别的学生人数大于3的信息
select cid,sGender,COUNT(*)
from StudentInfo
where sId>2
group by cid,sGender having COUNT(*)>3

 5----------------------------------.查询总结

 

-》总结:完整的select语句及执行顺序
select distinct top n *
from t1 join t2 on ... join t3 on ...
where ....
group by ... having ...
order by ...

--查询学生姓名、总分
--studentinfo scoreinfo
select stu.sName,SUM(score.scoreValue) as score1
from StudentInfo stu
inner join ScoreInfo score on stu.sId=score.stuId
group by stu.sName
order by score1 desc

 

 

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: