数据库系列学习(五)-数据的检索

1.执行本次学习的sql脚本

--创建学生表
create table T_Student
(
    --identity表示主键自增长,从1开始,且每次加1
    SId int primary key identity(1,1),
    SName nvarchar(10),
    SGender varchar(2) default(''),
    SAge int
)
--插入数据
insert into T_Student values('王五','',17)
insert into T_Student values('王五','',19)
insert into T_Student values('赵六','',17)
insert into T_Student values('Kim','',18)
insert into T_Student values('Lily','',18)
insert into T_Student values('Jerry','',17)

2.数据分组的group by

(1)简单的group by

通过一个例子,来了解group by为我们做了什么事

image

(2)带where子句的group by

image

【注】如果sql中有where子句,则group by 必须放到where语句之后

(3)group by指定多个列

image

(4)数据分组与聚合函数

image

(5)对分组结果进行过滤having 语句

image

3.限制结果集函数

(1)使用top 筛选

image

(2)使用开窗函数:row_number() over(排序规则)

image

3.抑制数据的重复

image

4.计算字段

(1)常量字段

image

(2)字段间的计算

image

5.数据处理函数

(1)字符串的拼接

image

6.联合查询

(1)union: 保证 联合后 数据的唯一性

image

(2)union all:保留 联合后 所有的数据

image

更多精彩内容请看:http://www.cnblogs.com/2star
原文地址:https://www.cnblogs.com/kimisme/p/4518461.html