如果有错误,可以在评论中评论或私信交流,会依据进行整理修改
– 1.学生表
Student(S,Sname,Sage,Ssex) – S 学生编号,Sname 学生姓名,Sage 出生年月,Ssex 学生性别
– 2.课程表
Course(C,Cname,T) – C – 课程编号,Cname 课程名称,T 教师编号
– 3.教师表
Teacher(T,Tname) – T 教师编号,Tname 教师姓名
– 4.成绩表
SC(S,C,score) – S 学生编号,C 课程编号,score 分数
*/
– 创建测试数据
create table Student(S varchar(10),Sname varchar(10),Sage datetime,Ssex nvarchar(10));
insert into Student values('01' , '赵雷' , '1990-01-01' , '男');
insert into Student values('02' , '钱电' , '1990-12-21' , '男');
insert into Student values('03' , '孙风' , '1990-05-20' , '男');
insert into Student values('04' , '李云' , '1990-08-06' , '男');
insert into Student values('05' , '周梅' , '1991-12-01' , '女');
insert into Student values('06' , '吴兰' , '1992-03-01' , '女');
insert into Student values('07' , '郑竹' , '1989-07-01' , '女');
insert into Student values('08' , '王菊' , '1990-01-20' , '女');
create table Course(C varchar(10),Cname varchar(10),T varchar(10));
insert into Course values('01' , '语文' , '02');
insert into Course values('02' , '数学' , '01');
insert into Course values('03' , '英语' , '03');
create table Teacher(T varchar(10),Tname varchar(10));
insert into Teacher values('01' , '张三');
insert into Teacher values('02' , '李四');
insert into Teacher values('03' , '王五');
create table SC(S varchar(10),C varchar(10),score decimal(18,1));
insert into SC values('01' , '01' , 80);
insert into SC values('01' , '02' , 90);
insert into SC values('01' , '03' , 99);
insert into SC values('02' , '01' , 70);
insert into SC values('02' , '02' , 60);
insert into SC values('02' , '03' , 80);
insert into SC values('03' , '01' , 80);
insert into SC values('03' , '02' , 80);
insert into SC values('03' , '03' , 80);
insert into SC values('04' , '01' , 50);
insert into SC values('04' , '02' , 30);
insert into SC values('04' , '03' , 20);
insert into SC values('05' , '01' , 76);
insert into SC values('05' , '02' , 87);
insert into SC values('06' , '01' , 31);
insert into SC values('06' , '03' , 34);
insert into SC values('07' , '02' , 89);
insert into SC values('07' , '03' , 98);
SELECT stu.*, sc.C,sc.score FROM student stu,sc sc1,sc sc2,sc sc
WHERE stu.S = sc.s AND stu.S = sc1.S AND stu.S = sc2.S AND sc1.C = '01' AND sc2.C = '02' AND sc1.score > sc2.score
SELECT stu.*, sc.C, sc.score FROM student stu, sc sc1, sc sc2, sc sc
WHERE stu.S = sc.s AND stu.S = sc1.S AND stu.S = sc2.S AND sc1.C = '01' AND sc2.C = '02' AND sc1.score < sc2.score
– 注:where后面不能有聚集函数,如sum(),avg()等,而having可以
SELECT stu.S AS '学号', stu.Sname AS '姓名', AVG(sc.score) AS '平均成绩' FROM student stu, sc sc
WHERE stu.S = sc.s GROUP BY sc.s HAVING AVG(sc.score) >= 60
SELECT stu.S AS '学号', stu.Sname AS '姓名', AVG(sc.score) AS '平均成绩'
FROM student stu, sc sc
WHERE stu.S = sc.s GROUP BY sc.s HAVING AVG(sc.score) < 60
SELECT
stu.s AS '学号',
stu.Sname AS '姓名',
COUNT(sc.C),
SUM(sc.score)
FROM
student stu,
sc sc
WHERE
1 = 1
AND stu.s = sc.s
GROUP BY
sc.s
SELECT
COUNT(*)
FROM
teacher t
WHERE
t.tname LIKE CONCAT('李', '%');
SELECT
stu.*
FROM
student stu,
sc sc,
teacher t
WHERE
1 = 1
AND stu.S = sc.s
AND sc.c = t.T
AND sc.C IN (
SELECT
c.C
FROM
teacher t,
course c
WHERE
t.T = c.T
AND Tname = '张三'
);
SELECT
*
FROM
student
WHERE
s NOT IN (
SELECT
stu.S
FROM
student stu,
sc sc,
teacher t
WHERE
1 = 1
AND stu.S = sc.s
AND sc.c = t.T
AND sc.C IN (
SELECT
c.C
FROM
teacher t,
course c
WHERE
t.T = c.T
AND Tname = '张三'
)
);
SELECT
stu.*
FROM
student stu,
sc sc1,
sc sc2
WHERE
stu.s = sc1.s
AND stu.s = sc2.s
AND sc1.c = '01'
AND sc2.c = '02';
SELECT
stu.*
FROM
student stu,
sc sc
WHERE
stu.s = sc.s
AND sc.c = '01'
AND NOT EXISTS (
SELECT
1
FROM
sc sc2
WHERE
sc2.s = sc.s
AND sc2.c = '02'
);
SELECT
stu.*
FROM
student stu,
sc sc
WHERE
sc.s = stu.s
GROUP BY
sc.s
HAVING
COUNT(sc.c) < (SELECT COUNT(*) FROM course);
SELECT DISTINCT
student.*
FROM
student,
sc
WHERE
student.S = sc.s
AND sc.c IN (SELECT c FROM sc WHERE s = '01')
AND student.s <> '01';
SELECT
s.*
FROM
(
SELECT
sc.s,
group_concat(sc.c ORDER BY c) AS c01
FROM
sc sc
WHERE
sc.s <> '01'
GROUP BY
sc.s
) a,
(
SELECT
group_concat(sc.c ORDER BY c) AS c01
FROM
sc sc
WHERE
sc.s = '01'
) b,
student s
WHERE
a.c01 = b.c01
AND a.s = s.s;
SELECT
stu.sname
FROM
student stu
WHERE
stu.s NOT IN (
SELECT DISTINCT
sc.s
FROM
sc sc,
course c,
teacher t
WHERE
sc.c = c.C
AND c.T = t.T
AND t.tname = '张三'
);
SELECT
s.s,
s.sname,
avg(sc.score)
FROM
student s,
sc sc
WHERE
s.s = sc.s
AND s.s IN (
SELECT
sc1.s
FROM
sc sc1
WHERE
sc1.score < 60
GROUP BY
sc1.s
HAVING
count(1) > 1
)
GROUP BY
s.s,
s.sname;
SELECT DISTINCT
s.*, sc.score AS score01
FROM
student s,
sc sc
WHERE
s.s = sc.s
AND sc.c = '01'
AND sc.score < 60
ORDER BY
sc.score DESC;
SELECT
a.s,
a.sname,
sum(IFNULL(a.score01, 0)) AS score01,
sum(IFNULL(a.score02, 0)) AS score02,
sum(IFNULL(a.score03, 0)) AS score03,
(
sum(IFNULL(a.score01, 0)) + sum(IFNULL(a.score02, 0)) + sum(IFNULL(a.score02, 0))
) / 3 AS avgScore
FROM
(
SELECT s.s,s.sname,
CASE
WHEN sc.c = '01' THEN
ifnull(sc.score, 0)
END AS score01,
CASE
WHEN sc.c = '02' THEN
ifnull(sc.score, 0)
END AS score02,
CASE
WHEN sc.c = '03' THEN
ifnull(sc.score, 0)
END AS score03
FROM
student s
LEFT JOIN sc sc ON s.s = sc.s
) a
GROUP BY
a.s,
a.sname
order by avgScore desc;
– 及格为>=60,中等为:70-80,优良为:80-90,优秀为:>=90
SELECT sc.c AS 课程ID, c.Cname AS 课程name, MAX(sc.score) AS 最高分, MIN(sc.score) AS 最低分, AVG(sc.score) AS 平均分,
( (SELECT COUNT(*) FROM sc sc1 WHERE sc1.C = sc.c AND sc1.score >= 60 ) / ( SELECT count(*) FROM student s, sc sc2 WHERE sc2.s = s.s AND sc2.c = c.c ) * 100 ) AS '及格率(%)',
( ( SELECT COUNT(*) FROM sc sc1 WHERE sc1.C = sc.c AND sc1.score >= 70 AND sc1.score < 80 ) / ( SELECT count(*) FROM student s, sc sc2 WHERE sc2.s = s.s AND sc2.c = c.c ) * 100 ) AS '中等率(%)',
( ( SELECT COUNT(*) FROM sc sc1 WHERE sc1.C = sc.c AND sc1.score >= 80 AND sc1.score < 90 ) / ( SELECT count(*) FROM student s, sc sc2 WHERE sc2.s = s.s AND sc2.c = c.c ) * 100 ) AS '优良率(%)',
( ( SELECT COUNT(*) FROM sc sc1 WHERE sc1.C = sc.c AND sc1.score >= 90 ) / ( SELECT count(*) FROM student s, sc sc2 WHERE sc2.s = s.s AND sc2.c = c.c ) * 100 ) AS '优秀率(%)'
FROM sc sc, course c WHERE 1 = 1 AND sc.c = c.c GROUP BY sc.c ORDER BY score;
# 实现的过程有点问题
CREATE view result as
SELECT stu.S,stu.Sname,c.Cname,sc.score
FROM student stu,sc sc,course c
WHERE stu.s = sc.s AND c.c = sc.c GROUP BY sc.c, sc.s;
SELECT tmp.S,tmp.Sname,tmp.Cname,tmp.score,@j :=@j + 1 AS j
FROM(SELECT * FROM result) AS tmp,(SELECT @j := 0 ,@pre_score := 0) sdcore
where tmp.Cname = '语文' order by tmp.score desc;
SELECT tmp.S,tmp.Sname,tmp.Cname,tmp.score,@j :=@j + 1 AS j
FROM(SELECT * FROM result ) AS tmp,(SELECT @j := 0 ,@pre_score := 0) sdcore
where tmp.Cname = '数学' order by tmp.score desc;
SELECT tmp.S,tmp.Sname,tmp.Cname,tmp.score,@j :=@j + 1 AS j
FROM(SELECT * FROM result ) AS tmp,(SELECT @j := 0 ,@pre_score := 0) sdcore
where tmp.Cname = '英语' order by tmp.score desc;
参考答案:
--19.1 sql 2000用子查询完成
--Score重复时保留名次空缺
select t.* , px = (select count(1) from SC where C = t.C and score > t.score) + 1 from sc t order by t.C , px
--Score重复时合并名次
select t.* , px = (select count(distinct score) from SC where C = t.C and score >= t.score) from sc t order by t.C , px
--19.2 sql 2005用rank,DENSE_RANK完成
--Score重复时保留名次空缺(rank完成)
select t.* , px = rank() over(partition by C order by score desc) from sc t order by t.C , px
--Score重复时合并名次(DENSE_RANK完成)
select t.* , px = DENSE_RANK() over(partition by C order by score desc) from sc t order by t.C , px
-- 创建视图
CREATE view result2 as
SELECT stu.S,stu.Sname,SUM(sc.score) AS score FROM student stu,sc sc WHERE stu.S = sc.s GROUP BY sc.S ORDER BY SUM(sc.score) DESC;
SELECT tmp.S,tmp.Sname,tmp.score,@j :=@j + 1 AS '排名'
FROM(SELECT * FROM result2) AS tmp,(SELECT @j := 0 ,@pre_score := 0) sdcore
SELECT t.Tname,c.Cname,(SUM(sc.score) / (SELECT COUNT(*) FROM sc sc1 WHERE sc1.c = sc.c) ) AS avgerage FROM teacher t,course c,sc sc WHERE t.T = c.T AND sc.C = c.C GROUP BY sc.c ORDER BY avgerage DESC;
create view result3 as
SELECT stu.*, c.Cname,sc.score FROM student stu,sc sc,course c WHERE stu.s = sc.s AND sc.C = c.C GROUP BY sc.c,sc.s;
select * from result3 where Cname = '语文' order by score desc limit 1,2;
select * from result3 where Cname = '数学' order by score desc limit 1,2;
select * from result3 where Cname = '英语' order by score desc limit 1,2;
SELECT c.C AS '课程编号',c.Cname AS '课程名称',
((SELECT count(1) FROM sc sc2 WHERE sc2.C = sc.c AND sc2.score >= 85) / (SELECT count(*) FROM student s,sc sc3 WHERE sc3.s = s.s AND sc3.c = c.c) *100) as '[85-100](%)',
((SELECT count(1) FROM sc sc2 WHERE sc2.C = sc.c AND sc2.score < 85 and sc2.score >= 70) / (SELECT count(*) FROM student s,sc sc3 WHERE sc3.s = s.s AND sc3.c = c.c) *100) as '[70-80](%)',
((SELECT count(1) FROM sc sc2 WHERE sc2.C = sc.c AND sc2.score <70 and sc2.score >= 60) / (SELECT count(*) FROM student s,sc sc3 WHERE sc3.s = s.s AND sc3.c = c.c) *100) as '[60-70](%)',
((SELECT count(1) FROM sc sc2 WHERE sc2.C = sc.c AND sc2.score <60) / (SELECT count(*) FROM student s,sc sc3 WHERE sc3.s = s.s AND sc3.c = c.c) *100) as '[0-60](%)'
FROM sc sc,course c WHERE sc.C = c.C GROUP BY sc.c;
select b.*,@j:=@j+1 as '排名' from (SELECT stu.S,stu.Sname,avg(sc.score) AS score FROM student stu,sc sc WHERE stu.S = sc.s GROUP BY sc.S ORDER BY avg(sc.score) DESC) as b,(SELECT @j := 0 ,@pre_score := 0) sdcore;
create view result4 as
SELECT stu.*, c.Cname,sc.score FROM student stu,sc sc,course c WHERE stu.s = sc.s AND sc.C = c.C GROUP BY sc.c,sc.s;
select * from result4 where Cname = '语文' order by score desc limit 0,3;
select * from result4 where Cname = '数学' order by score desc limit 0,3;
select * from result4 where Cname = '英语' order by score desc limit 0,3;
select c.cname as '课程',count(sc.s) as '人数' from sc sc,course c where sc.c = c.c group by sc.c
select s2.s,s2.sname from student s2,( select s.s,count(sc.c) as num from student s,sc sc where s.s=sc.s GROUP BY sc.s ) b where b.num =2 and b.s = s2.s;
select ssex as '性别',COUNT(1) as '人数' from student group by Ssex;
select s.* from student s where s.sname like '%风%';
select sname,ssex,count(sname) from student group by sname,ssex having count(sname) >1
select * from student where year(sage) = 1990
select c,avg(score) from sc group by c order by avg(score) desc,c
select b.* from (select s.s,s.sname,avg(sc.score) as avgScore from student s,sc sc where s.s = sc.s group by sc.s) b where b.avgScore >= 85
select s.sname,sc.score from student s,sc sc,course c where s.s=sc.s and sc.c = c.c and c.cname = '数学' and sc.score<60
select s2.s,s2.sname,b.cname,b.score from student s2 left join (select sc.s,s.sname,c.cname,sc.score from student s,sc sc,course c where s.s = sc.s and sc.c = c.c) b on s2.s = b.s
select s.sname,c.cname,sc.score from student s,sc sc,course c where s.s = sc.s and sc.c = c.c and sc.score >= 70
select s.sname,c.cname,sc.score from student s,sc sc,course c where s.s = sc.s and sc.c = c.c and sc.score < 60
select s.s,s.sname from student s,sc sc where s.s = sc.s and sc.c='01' and sc.score >= 80
select c.cname,COUNT(1) from sc sc,course c where sc.c =c.c group by sc.c
select s.*,sc.score from student s,sc sc,teacher t,course c where s.s = sc.s and c.t = t.t and sc.c = c.c and t.tname = '张三' order by sc.score desc limit 1
select sc.* from sc sc,(select sc1.c, sc1.score from sc sc1 group by sc1.c,sc1.score having count(1) > 1) n
where sc.C= n.C and sc.score = n.score order by sc.C , sc.score , sc.S
select b.* from (SELECT stu.*, c.Cname,sc.score FROM student stu,sc sc,course c WHERE stu.s = sc.s AND sc.C = c.C GROUP BY sc.c,sc.s) b where b.Cname = '语文' order by b.score desc limit 2;
select b.* from (SELECT stu.*, c.Cname,sc.score FROM student stu,sc sc,course c WHERE stu.s = sc.s AND sc.C = c.C GROUP BY sc.c,sc.s) b where b.Cname = '数学' order by b.score desc limit 2;
select b.* from (SELECT stu.*, c.Cname,sc.score FROM student stu,sc sc,course c WHERE stu.s = sc.s AND sc.C = c.C GROUP BY sc.c,sc.s) b where b.Cname = '英语' order by b.score desc limit 2;
select b.c,b.num from (select c,count(1) as num from sc group by c) b where b.num > 5 order by b.num desc,b.c asc
select b.s from (select s,count(1) as num from sc group by s) b where b.num >= 2
select s.* from student s,(select s,count(1) as num from sc group by s) b where s.s = b.s and b.num = (select count(1) as courseNum from course)
-- 只考虑年份
select s,sname,(select year(now()) - (select year(sage))) as age from student group by s
select * from student where ((select WEEKOFYEAR(now()))- (select WEEKOFYEAR(sage))) =0
select * from student where ((select WEEKOFYEAR(now()))- (select WEEKOFYEAR(sage))) = -1
select * from student where ((select month(now()))- (select month(sage))) = 0
select * from student where ((select month(now()))- (select month(sage))) = -1