0
点赞
收藏
分享

微信扫一扫

SQL数据库-子查询and视图

color_小浣熊 2022-05-06 阅读 60
数据库sql
--查询年龄比某某 大的学生信息
select * from Student
--先要查询到某某的年龄
select * from Student where sage>
(
select sage from Student where sname = '某某'
		--把查询到的结果做为条件 用括号括起来
)



--查询大于平均年龄的学生信息
select * from Student where sage>
(
	--先查询平均年龄
	select avg(sage) from Student
)


--in : 等于 or   在指定值内
select * from score

--查询参加考试的学生的信息
select * from Student where sid in (
	--查询到有成绩的学生学号
	select sid from score
)


--查询学号 为1 的学生信息
select * from Student where sid = 1

--查询学号 1和2 的学生信息
select * from Student where sid = 1 or sid = 2

--查询学号为 4、5、6、13、2、 号的学生信息
select * from Student where sid in(2,4,5,6,13)

--not in : 不包括
--查询没有考试的学生信息
select * from Student where sid not in (
	select sid from score
)


--view : 视图
--查询所有学生的信息个成绩(包括没考试的)
select * from Student a 
full join score b 
on a.sid=b.sid

--创建试图
create view V_T283
as
select a.sid 学号,a.sname 姓名,a.sage 年龄,a.ssex 性别,b.js 考试,b.bs  from Student a 
full join score b
on a.sid=b.sid


--使用视图 
select * from V_T283

--删除视图 
drop view V_T283
举报

相关推荐

0 条评论