关键问题:哪些表、关系(学生表的Id联系到班级表的Id)
内连接:inner join,两表中完全匹配的数据。
左外连接:left out join,两表中完全匹配的数据,左表中特有的数据,右表补全。
右外连接:right out join,两表中完全匹配的数据,右表中特有的数据,左表补全。
完全外连接:full out join ,两表中完全匹配的数据,左表中特有的数据,右表中特有的数据。
select *from gg inner join TextInfo on gg.Id=TextInfo.Id --将gg表中的Id列和ClassId表中的ClassId列对应
sum(综合)、avg(平均值)、count、max(最大值)、min(最小值)
一般是对数字类型的列进行操作
一条查询中可以同时写多个聚合函数,但不能与普通列混写。
聚合函数中的full问题:不参与运算。
select count(*) from TextInfo --查询TextInfo中有多少行
select count(*) from TextInfo where Id=3 --查询TextInfo中Id为3的有多少行
select max(Id) from gg --查询gg表中Id最大的行的Id
将统计出来的数据分布到原表的每一行中。
结合聚合函数、排名函数使用。
select gg.*,avg(Id) over() from gg --在gg表中新增一列平均值,加上over()
聚合函数一般结合分组使用,对分组内的数据进行统计。
根据指定列进行分组。
分组后条件筛选:having... select sex,count(*) from TextInfo group by sex --将数据按照sex(性别)来分组,再统计每组人数
select sex,pwd,count(*) from TextInfo group by sex,pwd --将数据按照sex(性别)来分组,在进行pwd分组,最后统计每组人数
--统计Id大于3的姓名为玄武的人数 select count(*) from gg where [Name]='玄武' and Id>3 group by [Name]
select distinct top n *
from t1 join t2 on ...join t3 on ...
where ...
group by ...having....
order by...