今天下午快下课时,老师出了3道题,唉,弟弟的我,第一题都是取巧的方法做的,现在我把题目放上来,还是有点意思的.
ps:我在做的时候,好多东西都忘记了,跟吃了忘记药一样
pss:限制的使用顺序:SELECT … FROM … WHERE … GROUP BY … ORDER BY … 还是很有用的
1.用一条SQL 语句 查询出每门课都大于80 分的学生姓名
name kecheng fenshu
张三 语文 81
张三 数学 75
李四 语文 76
李四 数学 90
王五 语文 81
王五 数学 100
王五 英语 90
答案:
select name from student where fenshu>80 group by name having count(name)>1;
解析:没啥好讲的,pss:限制的使用顺序:SELECT … FROM … WHERE … GROUP BY … ORDER BY … 记住这个就行
2.怎么把这样一个表儿(使用嵌套循环)
year month amount
1991 1 1.1
1991 2 1.2
1991 3 1.3
1991 4 1.4
1992 1 2.1
1992 2 2.2
1992 3 2.3
1992 4 2.4
查成这样一个结果
year m1 m2 m3 m4
1991 1.1 1.2 1.3 1.4
1992 2.1 2.2 2.3 2.4
答案
select year,
(select amount from aaa m where month=1 and m.year=aaa.year) as m1,
(select amount from aaa m where month=2 and m.year=aaa.year) as m2,
(select amount from aaa m where month=3 and m.year=aaa.year) as m3,
(select amount from aaa m where month=4 and m.year=aaa.year) as m4
from aaa group by year
3.为管理业务培训信息,建立3个表:
S(S#,SN,SD,SA)S#,SN,SD,SA分别代表学号,学员姓名,所属单位,学员年龄
C(C#,CN)C#,CN分别代表课程编号,课程名称
SC(S#,C#,G) S#,C#,G分别代表学号,所选的课程编号,学习成绩
(1)使用标准SQL嵌套语句查询选修课程名称为’税收基础’的学员学号和姓名?
(2) 查询选修课程编号为’C2’的学员姓名和所属单位
(3) 使用标准SQL嵌套语句查询不选修课程编号为’C5’的学员姓名和所属单位?
(4) 查询选修了课程的学员人数
(1)select s# ,sn from s where S# in(select S# from c,sc where c.c#=sc.c# and cn=’税收基础’)
(2)select sn,sd from s,sc where s.s#=sc.s# and sc.c#=’c2’
(3)select sn,sd from s where s# not in(select s# from sc where c#=’c5’)
(4)select 学员人数=count(distinct s#) from sc