--查询没有被删除的学生

alter table StuInfo --修改列属性
alter column isdelete bit null alter table StuInfo --删除列
drop column isdelete alter table StuInfo --增加列
add isdelete bit not null default(0) select * from StuInfo as si
inner join Classinfo as ci on si.classId=ci.classId
where isdelete=0 --创建视图,用于二次查询
create view StuInfo_Class
as
select si.*,ci.className from StuInfo as si
inner join Classinfo as ci on si.classId=ci.classId
where isdelete=0 --视图中存储的是select语句,而不是结果集合
select * from StuInfo_Class drop view StuInfo_Class -- 删除 select * from StuInfo
select * from ScoreInfo --查询参与了考试的学生信息,子查询exists,in
select * from StuInfo
where stuId in(select distinct stuId from ScoreInfo) select * from StuInfo as si
where exists
(select * from ScoreInfo as sco where si.stuId=sco.StuId) --分页 已知:页大小(一页显示多少数据),页索引
-- 3 1,2,3,4
--利用子查询进行分页:
select * from
(
select *,
ROW_NUMBER()over(order by stuPhone asc) as rowIndex
from StuInfo
) as t1
where rowIndex between 1 and 3 --先排序后再查询在一个区间的列 --当前学生的科目成绩
create view S_S_C
as
select si.*,ci.cName,sco.score from StuInfo as si
inner join ScoreInfo as sco on si.stuId=sco.StuId
inner join CourseInfo as ci on ci.cId=sco.cId select * from S_S_C --数据透视
--要求按格式:姓名 数据库 算法设计
select stuName as 姓名,
max(case cName when '数据库' then score end) as 数据库, --case..when..then语句的两种写法
max(case when cName='算法设计' then score end) as 算法设计
from S_S_C
--然后还要用聚合函数max|sum将上面的结果合并起来,去掉null的位置
group by stuName

接下去是一些练习

select * from course
select * from score
select * from student
select * from teacher --[20,25]岁男同学的姓名,性别,年龄
create view student_view
as
select stuName,stuAge,stuSexy from student
where stuAge between 20 and 25 select * from student_view --每位同学姓名,课程名称和成绩
create view score_view
as
select si.stuName,ci.cName,sco.score from student as si
inner join score as sco on si.stuId=sco.stuId
inner join course as ci on sco.cId=ci.cId select * from score_view

关于透视

select * from class
insert into class
values('','大数据') select * from course
select * from student
select * from teacher
select * from score --透视练习:按课程名字 男生 女生
create view Cou_Stu
as
select si.stuId,ci.cName,si.stuSexy from student as si
inner join score as sco on si.stuId=sco.stuId
inner join course as ci on sco.cId=ci.cId drop view Cou_Stu
select * from Cou_Stu create view TMP
as
select cName,stuSexy,COUNT(*)as num from Cou_Stu
group by stuSexy,cName select * from TMP select cName,
(case when stuSexy=1 then num end)as '男生',
(case when stuSexy=0 then num end)as '女生'
from TMP --再通过分组+聚合函数把null给去了
select cName,
max(case when stuSexy=1 then num end)as '男生',
max(case when stuSexy=0 then num end)as '女生'
from TMP
group by cName

最新文章

  1. java-JDBC配置驱动程序
  2. Android 摇一摇功能的注意事项
  3. MongoDB初步(一)
  4. 封装实现一个自己的tabbar
  5. iOS 直播(一)
  6. Linux 磁盘与文件系统管理
  7. [codevs1557]热浪
  8. Pencil OJ 01 开发的准备
  9. HDU 4974 A simple water problem(贪心)
  10. java通过抛异常来返回提示信息
  11. computer专业术语总结
  12. 总结 React 组件的三种写法 及最佳实践 [涨经验]
  13. CodeForces 706E Working routine
  14. openfire源码解读--用户登录
  15. 爬取博主所有文章并保存到本地(.txt版)--python3.6
  16. BZOJ 1053 - 反素数ant - [数论+DFS][HAOI2007]
  17. shell编程第三天
  18. kafka server管理
  19. ref与out
  20. sqlite3错误码整理

热门文章

  1. 【bzoj1185】[HNOI2007]最小矩形覆盖 (旋转卡壳)
  2. hdu1166:敌兵布阵(树状数组或线段树)
  3. php基于SQLite实现的分页功能示例
  4. python魔法方法__reduce__()的妙用
  5. codeforces gym 100345I Segment Transformations [想法题]
  6. STM32几个IO的工作模式
  7. Iconv作用以及安装问题解决
  8. 在Windows及Linux下获取毫秒级运行时间的方法
  9. 3.tensorflow——NN
  10. springboot 尚桂谷学习笔记03