欢迎投稿

今日深度:

第六单元 简单查询,。。条selectt

第六单元 简单查询,。。条selectt


create database step2;
go
use step2;
go-- 学生表
create table StudentInfo
(
    stuId char(10) primary key, -- 主键
    stuName varchar(20), -- 姓名
    ClassId int,  -- 班级编号,逻辑外键,并不是真正的外键约束
    stuPhone char(11), -- 电话号码
    stuSex char(4),  -- 性别
    stuBirthday datetime  -- 生日
);
go
-- 班级表
create table ClassInfo
(
    Id int primary key identity, -- 班级的主键
    Name varchar(30),   -- 班级名称
    College varchar(20)  -- 学院
);
go
-- 成绩表
create table StudentScore
(
    Id int primary key identity,  -- 成绩的主键
    stuId char(10),  -- 学生外键
    CourseName varchar(20), -- 课程
    theoryScore int,  -- 理论成绩
    skillScore int   -- 技能成绩
);
​
INSERT INTO dbo.StudentInfo(stuId,stuName,ClassId,stuPhone,stuSex,stuBirthday)VALUES
('180325011','任我行',5,'13823204456','', '1999-09-09'),
('180325012','张三',4,'13823204452','', '1998-08-08'),
('180325013','李四',2,'18899251152','', '1997-07-07'),
('180325014','王五',1,'13597445645','', '1998-08-08'),
('180325015','帅天行',5,'13814204456','', '1998-06-06'),
('180325016','叶星辰',5,'17623204936','', '1998-05-05'),
('180325017','赵日天',0,'13922044932','', '1997-07-15');
go
INSERT INTO dbo.ClassInfo(Name,College)VALUES
('软件技术1班', '计算机系'  ),
('会计1班', '经济管理系'  ),
('会计2班', '经济管理系'  ),
('欧美软件外包班', '计算机系'  ),
('会计3班', '经济管理系'  );
go
INSERT INTO dbo.StudentScore(stuId,CourseName,theoryScore,skillScore)VALUES
(   '180325011', '会计从业', 80,  90  ),
(   '180325011', 'C# 入门编程', 99,  100  ),
(   '180325012', 'SQLServer编程', 70,  75  ),
(   '180325013', '会计从业', 93,  80  ),
(   '180325014', 'C# 高级编程', 99,  99  ),
(   '180325015', '会计从业', 74,  40  ),
(   '180325015', 'C# 入门编程', 80,  90  );
 

​
--1.如何查看表中所有数据?
-- 查看学生表
-- select: 查询
-- *:代表表中所有的列
select * from StudentInfo
-- * 号,在数据库优化的章节中,不建议使用*号,因为系统要解析这个*号,需要一点点时间
-- 实际开发中,如果字段过多,我们查询时,只查出业务中所需要的字段
select stuName,stuId from StudentInfo
​
​
-- 查询班级表,执行
select  id,Name,College  from ClassInfo
​
​
--2.如何查询指定几个字段的值?
-- 查询学生的姓名,性别,生日,班级
select stuName,stuSex,stuBirthday,ClassId  from StudentInfo
​
--3.如何给字段取别名?(可以省略as)
-- 把学生表中所有的字段都取别名
select stuId as 学生主键,stuName as 姓名,ClassId 班级编号,stuPhone 电话号码,
stuSex 性别,stuBirthday 生日
from StudentInfo
​
--4.distinct的用法?多个字段的用法?
 -- distinct:去除重复项
 select distinct ClassId from StudentInfo
 select distinct stuSex from StudentInfo
 -- 指的是两个字段组合在一起,不会重复
 select distinct stuSex,ClassId from StudentInfo
​
-- 这两个结果集为什么会一样?
select stuId,CourseName from StudentScore
-- disctinct 后面跟着几个字段,表示 去除这几个字段(组合在一起)重复的意思
select distinct stuId,CourseName from StudentScore
​
-- 这样写会去除重复吗?
select distinct * from StudentInfo  -- 这样写没有意义,反而增加系统的开销
--5.top 的用法?
 -- 取前3条数据
 -- top :前。。。条
 select top 3 * from StudentInfo
​
​
--6.top ... percent(百分比)?
-- 查询前30%的学生数据
-- 从这个故事告诉我们,数据没有半条,向上取整
select top 30 percent *  from StudentInfo
​
​
--7.查询年龄大于20岁的?
-- year():获取年份
-- 年龄 = 当前年份 - 生日所在年份
select * from StudentInfo where (year(getdate())-year(stuBirthday))>20
​
​
-- 查询学生的,姓名,性别,年龄  所有字段取别名
-- 年龄 = 当前年份 - 生日所在年份
select stuName as 姓名,stuSex as 性别,(year(getdate())-year(stuBirthday)) as 年龄  from StudentInfo
​
-- 查询80 后的女生
-- 80后:1980-1989
select * from StudentInfo where year(stuBirthday)>=1980 and year(stuBirthday)<=1989
and stuSex=''
​
​
--9.查询姓李的学生信息
-- like:模糊查询,中文意思是:像。。。
select * from StudentInfo where stuName like '李%'--10.列出技能成绩大于90分的成绩单
select * from StudentScore where skillScore>=90
​
​
--11.查询课程包含”SqlServer”的成绩信息
select * from StudentScore where CourseName like '%SqlServer%'
​
​
--12.查询每个学生不同的成绩列表
select distinct stuId,skillScore from StudentScore
​
​
--15.查询年龄大于20岁前3条学生的姓名,年龄,所有字段取别名
select top 3 stuName as 姓名,stuSex as 性别  from StudentInfo where (year(getdate())-year(stuBirthday))>20
 
 

 

 

配套视频链接:SQLServer 入门基础 - 网易云课堂 (163.com)

海阔平鱼跃,天高任我行,给我一片蓝天,让我自由翱翔。

www.htsjk.Com true http://www.htsjk.com/Sql_Server/47329.html NewsArticle 第六单元 简单查询,。。条selectt create database step2; go use step2; go ​ -- 学生表 create table StudentInfo( stuId char ( 10 ) primary key , -- 主键 stuName varchar ( 20 ), -- 姓名 ClassId int , -- 班级编号,逻辑...
相关文章
    暂无相关文章
评论暂时关闭