单表查询
-- 查询2016年~2017年入学的记录select * from studentwhere s_regDatebetween '2016-1-1'and '2017-12-31'select * from studentwhere s_regDate >= '2016-1-1'and s_regDate <= '2017-12-31'select s_id as 学号, s_regDate 入学日期, year(s_regDate) as 年份 , MONTH(s_regDate) as 月份 from studentwhere year(s_regDate)between 2016and 2017-- 查询姓张的学生记录select * from student where s_name like '张%' -- % :任意数量的任意字符select * from student where s_name like '张_' -- _ : 一个数量的任意字符-- 排序的查询输出select * from student order by s_id desc -- desc :倒序, asc :正序(默认)select * from student order by s_name descselect s_id, s_class from studentorder by s_class ,s_id desc -- 多值排序的规则:先按班级排序,如果班级有相同,则按学号排序
多表查询
-- 查询有成绩的学生信息和分数-- 写法1:select a.s_id 学号, s_name 姓名, c_id 课程ID, sc_score 分数from student a , score b -- 需要查询的多张表,用逗号分开,可以为每张表分配一个别名where a.s_id = b.s_id -- 连接关系:主表.主键 = 从表.外键-- 写法2:select a.s_id 学号, s_name 姓名, c_id 课程ID, sc_score 分数from student a join score b -- join : 表示 inner join 内链接on a.s_id = b.s_id -- on :建立表连接关系-- 外连接:查询所有的学生信息和分数select a.s_id 学号, s_name 姓名, c_id 课程ID, sc_score 分数from student a left join score b -- left join : 表示 左外连接on a.s_id = b.s_id -- on :建立表连接关系select a.s_id 学号, s_name 姓名, c_id 课程ID, sc_score 分数from student a right join score b -- right join : 表示 右外连接on a.s_id = b.s_id
外连接查询结果的NULL处理
ISNULL是SQLServer的内置函数,表示如果当前的字段是NULL,则用默认值代替输出
-- 外连接查询如何处理查询结果的NULL值select a.s_id 学号, s_name 姓名,ISNULL(convert(varchar(10),c_id),'无') 课程ID,ISNULL(convert(varchar(10),sc_score) ,'未考') 分数 -- 每一列的数据类型要相同,所以类型转换from student a left join score b -- 左外连接on a.s_id = b.s_id
查询里面的条件判断 case ... when 语句
格式:
case when 字段的判断条件1 then 输出结果1when 字段的判断条件2 then 输出结果2else 默认结果end
案例:输出成绩的评定结果
select s_id 学号, c_id 课程ID,case when sc_score<60 then '不及格' -- if (分数低于60) 输出“不及格”else convert(varchar(10), sc_score) -- else 输出分数end 分数from score-- 根据成绩输出对应的级别select *, -- 输出原有的score的字段case when sc_score>=90 then '优' -- if (分数>=90)when sc_score>=80 then '良' -- else if (分数>=80)when sc_score>=70 then '中' -- else if (分数>=70)when sc_score>=60 then '及格' -- else if (分数>=60)else '不及格' -- esle 默认值end 分数级别 -- 动态创建的列from score
统计查询
-- 统计总共有多少个班级select count(distinct s_class) 班级数量 from student -- distinct去掉查询结果的重复项-- 统计学号ST001的平均分select AVG(sc_score) 平均分 from score where s_id='ST001'-- 统计每个人平均分select AVG(sc_score) 平均分 , s_id 学号 -- 包含非统计字段的查询都必须使用分组from scoregroup by s_id -- 分组统计-- 统计各班男女生人数select count(s_id) 人数, s_class 班级, s_sex 性别 -- 在select部分包含两个非统计字段班级和性别from student -- 所以这个两个字段必须要参与分组group by s_class, s_sex -- 按班级和性别分组order by s_class -- 排序班级再输出-- 统计没有参加考试的人数select count(*)from student left join score -- 查询没有参加考试的人(外连接)on student.s_id = score.s_id -- 连接关系where sc_score is null -- 分数为null的是未考
子查询
子查询的三种格式:
子查询位于条件部分:select * from 表 where 字段 in (子查询)
子查询位于表部分: select * from 表, (子查询) t where 表.关系字段 = t.关系字段
子查询位于select部分:select *, (子查询) from 表
-- 子查询最高分的学生记录select * from student where s_id in(select s_id from score where sc_score = (select max(sc_score) from score))-- 统计每门课的最高分的学号select s_idfrom score,(select max(sc_score) as m , c_idfrom scoregroup by c_id) twhere score.c_id = t.c_id -- 连接关系1:分数表.课程ID = t.课程IDand score.sc_score = t.m -- 连接关系2:分数表.分数 = t.最高分
联合查询:union
-- 输出成绩、小计平均分、总分select a.s_id as 学号, s_name as 姓名, c_name as 课程名, sc_score as 分数from student a, course b, score cwhere a.s_id = c.s_idand b.c_id = c.c_idunionselect s_id,'小计:','平均分:',avg(sc_score)from scoregroup by s_idunionselect s_id,'小计:','总分:',sum(sc_score)from scoregroup by s_id
文章转载自全栈精英,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。




