之前联系了一些MySQL的查询相关知识,现在补充作为一个记录,免得自己忘记。

致谢博主:https://blog.csdn.net/dehu_zhou/article/details/52881587

#建表语句与测试数据
--创建测试数据
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 SC(
Sid varchar(10),
Cid 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); create table Course(
Cid varchar(10),
Cname varchar(10),
Tid varchar(10)
); insert into Course values('01' , '语文' , '02');
insert into Course values('02' , '数学' , '01');
insert into Course values('03' , '英语' , '03'); create table Teacher(
Tid varchar(10),
Tname varchar(10)
); insert into Teacher values('01' , '张三');
insert into Teacher values('02' , '李四');
insert into Teacher values('03' , '王五');

  练习题:

    题一:查询"01"课程比"02"课程成绩高的学生的信息及课程分数。  

--分析过程:
--基表:学生的信息(Student),课程分数(SC)
--条件:
#1.成绩自己比自己;
#2.查询学生的信息;
#方法1:使用内连接,并作为子查询条件进行查询
SELECT
t2.*,
t1.`01分数`,
t1.`02分数`
FROM
(
SELECT
a.Sid,
a.score AS '01分数',
b.score AS '02分数'
FROM
SC a
INNER JOIN SC b ON a.Sid = b.Sid and a.Cid='01' and b.Cid='02'
WHERE a.score > b.score
) t1,
Student t2
where t1.Sid=t2.Sid;
#方法2:使用三层内连接连续使用
#考点:三层嵌套内连接
SELECT
a.*,
b.score AS '01分数',
c.score AS '02分数'
FROM
Student a
INNER JOIN SC b ON a.Sid = b.Sid
AND b.Cid = '01'
INNER JOIN SC c ON a.Sid = c.Sid
AND c.Cid = '02'
WHERE
b.score > c.score;

    2、查询"01"课程比"02"课程成绩低的学生的信息及课程分数

#与题目1一致,修改成绩比较条件

SELECT
a.*, b.score AS '01分数',
c.score AS '02分数'
FROM
Student a
INNER JOIN SC b ON a.Sid = b.Sid
AND b.Cid = '01'
INNER JOIN SC c ON a.Sid = c.Sid
AND c.Cid = '02'
WHERE
b.score < c.score;

    3、查询平均成绩大于等于60分的同学的学生编号和学生姓名和平均成绩

#考点,group by 子句以及having的条件删选

SELECT
a.*, b.avg_score
FROM
Student a,
(
SELECT
t.Sid,
round(avg(t.score), 2) AS avg_score
FROM
SC t
GROUP BY
t.Sid
HAVING
avg(t.score) > 60
) b
WHERE
a.Sid = b.Sid; #+外连接版本
SELECT
t1.*,t2.avg_score
FROM
(
SELECT
t.Sid,
round(avg(t.score), 2) AS avg_score
FROM
SC t
GROUP BY
t.Sid
HAVING
avg(t.score) > 60
) t2
LEFT JOIN Student t1 ON t2.Sid = t1.Sid; #外连接凝练版本
SELECT
t1.*, round(avg(t.score), 2) AS avg_score
FROM
SC t
RIGHT JOIN Student t1 ON t.Sid = t1.Sid
GROUP BY
t.Sid
HAVING
avg(t.score) > 60;

    4、查询平均成绩小于60分的同学的学生编号和学生姓名和平均成绩

SELECT
t1.*, round(avg(t.score), 2) AS avg_score
FROM
SC t
RIGHT JOIN Student t1 ON t.Sid = t1.Sid
GROUP BY
t.Sid
HAVING
avg(t.score) < 60;

    5、查询所有同学的学生编号、学生姓名、选课总数、所有课程的总成绩  

SELECT
b.Sname,
a.Sid,
count(DISTINCT a.Cid) as '选课总数',
sum(a.score) as '总成绩'
FROM
SC a
LEFT JOIN Student b ON a.Sid = b.Sid
GROUP BY
a.Sid;

    6、查询"李"姓老师的数量

select count(*) from Teacher a where a.Tname like '李%';

    7、查询学过"张三"老师授课的同学的信息

  

SELECT
a.Tname,
b.Cid,
d.*
FROM
Teacher a,
Course b,
SC c,
Student d
WHERE
a.Tname = '张三'
AND a.Tid = b.Tid
AND b.Cid = c.Cid
AND c.Sid = d.Sid; #方法2:使用三层内连接连续使用
SELECT
a.*
FROM
Student a
INNER JOIN SC b ON a.Sid = b.Sid
INNER JOIN Course c ON b.Cid = c.Cid
INNER JOIN Teacher d ON c.Tid = d.Tid
WHERE
d.Tname = '张三'
GROUP BY
1,2,3,4; #按照查询(select后面的)的1,2,3,4字段来分组 #方法3:方法2基础上补充
SELECT
a.*
FROM
Student a
LEFT JOIN (
SELECT
a.*
FROM
Student a
INNER JOIN SC b ON a.Sid = b.Sid
INNER JOIN Course c ON b.Cid = c.Cid
INNER JOIN Teacher d ON c.Tid = d.Tid
WHERE
d.Tname = '张三'
GROUP BY
1,2,3,4
) t ON a.Sid = t.Sid
WHERE
t.Sid IS NOT NULL;

    8、查询没学过"张三"老师授课的同学的信息

#方法1:直接对原来学过张三老师课程的学生信息进行not in

SELECT
*
FROM
Student t
WHERE
t.Sid NOT IN (
SELECT
d.Sid
FROM
Teacher a,
Course b,
SC c,
Student d
WHERE
a.Tname = '张三'
AND a.Tid = b.Tid
AND b.Cid = c.Cid
AND c.Sid = d.Sid
);
#NOT IN 与 NOT EXISTS的用法区别
SELECT
*
FROM
Student t
WHERE
not EXISTS (
SELECT
* #此处返回的是整个结果集,如果单挑sid,则返回的结果与预期结果不一致
FROM
Teacher a,
Course b,
SC c,
Student d
WHERE
a.Tname = '张三'
AND a.Tid = b.Tid
AND b.Cid = c.Cid
AND c.Sid = d.Sid
AND d.Sid = t.Sid #此处为结果集的判断
);

  9、查询学过编号为"01"并且也学过编号为"02"的课程的同学的信息

SELECT
c.*,a.score as '01分数',b.score as '02分数'
FROM
Student c
INNER JOIN SC a ON c.Sid = a.Sid
INNER JOIN SC b ON a.Sid = b.Sid
AND a.Cid = '01'
AND b.Cid = '02';

  10、查询学过编号为"01"但是没有学过编号为"02"的课程的同学的信息

#重要,我没有做出来
##方法:连续的左连接,筛选出差异结果
SELECT
*
FROM
Student a
LEFT JOIN SC b ON a.Sid = b.Sid
AND b.Cid = '01'
LEFT JOIN SC c ON a.Sid = c.Sid
AND c.Cid = '02'
WHERE
b.Cid = '01'
AND c.Cid IS NULL;

  11、查询没有学全所有课程的同学的信息

SELECT
*
FROM
Student b
LEFT JOIN SC a ON a.Sid = b.Sid
GROUP BY
a.Sid
HAVING
count(a.cid) < 3; #完善方法
SELECT
a.*
FROM
Student a
LEFT JOIN SC b ON a.Sid = b.Sid
LEFT JOIN (
SELECT
COUNT(1) anum
FROM
Course
) c ON 1 = 1 #通过子查询和恒真条件筛选
GROUP BY
1, 2, 3, 4
HAVING
MAX(c.anum) > COUNT(b.Cid);

    12、查询至少有一门课与学号为"01"的同学所学相同的同学的信息

SELECT DISTINCT
b.*
FROM
Student b
LEFT JOIN SC c ON b.Sid = c.Sid
AND c.Cid IN (
SELECT
a.Cid
FROM
SC a
WHERE
a.Sid = '01'
); #方法2:
SELECT
a.*
FROM
Student a
INNER JOIN SC b ON a.Sid = b.Sid
WHERE
EXISTS (
SELECT
1
FROM
SC
WHERE
Sid = '01'
AND Cid = b.Cid
)
GROUP BY
1, 2, 3, 4; #方法3::通过组装cid字段比对进行筛选
#重点:GROUP_CONCAT(cid ORDER BY cid) 函数的用法
#1.需要配合group by 子句使用
#2.内部字段的排序的用法 SELECT DISTINCT
t.*
FROM
Student t
INNER JOIN SC t1 ON t.Sid = t1.Sid
WHERE
t.Sid IN (
SELECT
t3.Sid
FROM
(
SELECT
sid,
GROUP_CONCAT(cid ORDER BY cid) AS cid_list
FROM
SC a
WHERE
a.sid = '01'
GROUP BY
sid
) t2
RIGHT JOIN (
SELECT
sid,
GROUP_CONCAT(cid ORDER BY cid) AS cid_list
FROM
SC a
GROUP BY
sid
) t3 ON t2.cid_list = t3.cid_list
WHERE
t2.sid IS NOT NULL
);

    14、查询没学过"张三"老师讲授的任一门课程的学生姓名

SELECT
t.*, t1.Tid
FROM
Student t
LEFT JOIN (
SELECT
a.*, d.Tid
FROM
Student a
LEFT JOIN SC b ON a.sid = b.sid
LEFT JOIN Course c ON c.Cid = b.Cid
LEFT JOIN Teacher d ON c.Tid = d.Tid
AND d.Tname = '张三'
WHERE
d.Tid IS NOT NULL
) t1 ON t.Sid = t1.sid
WHERE
t1.tid IS NULL;

    15、查询两门及其以上不及格课程的同学的学号,姓名及其平均成绩

SELECT
*
FROM
Student t
LEFT JOIN (
SELECT
sid,
round(avg(score), 1) AS avg_score
FROM
SC a
WHERE
a.score < 60
GROUP BY
sid
HAVING
count(*) > 1
) t1 ON t.Sid = t1.sid
WHERE
t1.avg_score IS NOT NULL;
#优化
SELECT
a.Sid       ,
a.Sname       ,
round(avg(score), 1) AS avg_score
FROM
Student a
INNER JOIN SC b ON a.Sid = b.Sid
GROUP BY
1,
2
HAVING
SUM(
CASE
WHEN b.Score >= 60 THEN
0
ELSE
1
END
) >= 2;

    16、检索"01"课程分数小于60,按分数降序排列的学生信息

SELECT
a.*
FROM
Student a
LEFT JOIN SC b ON a.Sid = b.Sid
AND b.Cid = '01'
WHERE
b.score < 60
ORDER BY
b.Cid DESC;

    17、按平均成绩从高到低显示所有学生的所有课程的成绩以及平均成绩

select t.*, round((s01+s02+s03)/3,2) as avs from (
SELECT a.*
,SUM(CASE WHEN b.Cid='01' THEN b.score ELSE 0 END) AS s01
,SUM(CASE WHEN b.Cid='02' THEN b.score ELSE 0 END) AS s02
,SUM(CASE WHEN b.Cid='03' THEN b.score ELSE 0 END) AS s03
FROM Student a
LEFT JOIN SC b
ON a.Sid=b.Sid
GROUP BY 1,2,3,4
) t ORDER BY avs desc; select * from Student t
LEFT JOIN (
select a.Sid
,max(case when a.Cid='01' THEN a.score ELSE 0 END) as s01
,max(case when a.Cid='02' THEN a.score ELSE 0 END) as s02
,max(case when a.Cid='03' THEN a.score ELSE 0 END) as s03
,round(avg(case when a.score is null then 0 else a.score end),2) as avs
from SC a group BY a.Sid ) t1
on t.Sid = t1.Sid
;

    18、查询各科成绩最高分、最低分和平均分:以如下形式显示:课程ID,课程name,最高分,最低分,平均分,及格率,中等率,优良率,优秀率

#-- --及格为>=60,中等为:70-80,优良为:80-90,优秀为:>=90
#考察重点再分组 select
a.cid,b.Cname,max(a.score),min(a.score),avg(a.score)
,concat(ROUND(count(IF(a.score >=60 ,1,null))/count(1) * 100,2),'%') as '及格率'
,concat(ROUND(sum(case when a.score >= 70 and a.score < 80 then 1 else 0 end)/count(1) * 100,2),'%') as '中等率'
,concat(ROUND(sum(case when a.score >= 80 and a.score < 90 then 1 else 0 end)/count(1) * 100,2),'%') as '优良率'
,concat(ROUND(sum(case when a.score >= 90 then 1 else 0 end)/count(1) * 100,2),'%') as '优秀率'
from SC a
INNER JOIN Course b on a.Cid = b.Cid group by 1,2;

    19、按各科成绩进行排序,并显示排名

#单科排序方法,自定义变量
SET @rn:=0;
SELECT a.*,@rn:=@rn+1 as '排名'
FROM(
select a.*,b.score from Course a INNER JOIN SC b on a.Cid = b.Cid where a.Cid='01' ORDER BY a.Cid,b.score desc
)a ;
#按科目排序方法,通过自连接比对排序规则
SELECT
a.*, count(b.Cid) + 1 AS tp
FROM
SC a
LEFT JOIN SC b ON a.Cid = b.Cid
AND a.score < b.score
GROUP BY
a.Sid,
a.Cid
ORDER BY
a.Cid,
a.score DESC;

    20、查询学生的总成绩并进行排名

SET @ct := 0;

SELECT
(@ct := @ct + 1) AS '排名',
b.Sname,
ifnull(sum(a.score), 0) AS '总成绩'
FROM
SC a
RIGHT JOIN Student b ON a.Sid = b.Sid
GROUP BY
a.Sid
ORDER BY
sum(a.score) DESC;

    21、查询不同老师所教不同课程平均分从高到低显示

##没有统计未参考学生的平均分
select d.Tname,c.Cname,ROUND(sum(b.score)/count(a.Sid),2) as '科目平均分'
from Student a LEFT JOIN SC b on a.Sid = b.Sid
LEFT JOIN Course c on c.Cid = b.Cid
LEFT JOIN Teacher d on c.Tid = d.Tid
where b.Cid is not null group by b.Cid ORDER BY sum(b.score)/count(a.Sid) desc; SELECT a.*,b.Cname,AVG(c.Score) ascore
FROM Teacher a
INNER JOIN Course b
ON a.Tid=b.Tid
INNER JOIN SC c
ON b.Cid=c.Cid
GROUP BY 1,2,3
ORDER BY ascore DESC ;
#统计未参考学生的平均分
select
sum(case when b.cid = '01' then b.score else 0 end)/count(distinct a.Sid) as avg_01,
sum(case when b.cid = '02' then b.score else 0 end)/count(distinct a.Sid) as avg_02,
sum(case when b.cid = '03' then b.score else 0 end)/count(distinct a.Sid) as avg_03 from Student a LEFT JOIN SC b on a.Sid = b.Sid;

    22、查询所有课程的成绩第2名到第3名的学生信息及该课程成绩

#核心为题19按科目的成绩排序
SELECT
c.Sname,
d.cid,
d.score,
d.tp
FROM
Student c
INNER JOIN (
SELECT
a.*, count(b.Cid) + 1 AS tp
FROM
SC a
LEFT JOIN SC b ON a.Cid = b.Cid
AND a.score < b.score
GROUP BY
a.Sid,
a.Cid
HAVING
count(b.Cid) + 1 IN (2, 3)
) d ON c.Sid = d.sid;

    23、统计各科成绩各分数段人数:课程编号,课程名称,[100-85],[85-70],[70-60],[0-60]及所占百分比

select  t.*,c.Cname
,concat(round(t.bjg/sum(t.bjg+t.pt+t.lh+t.yx)* 100 ,2),'%') as bl_bjg
,concat(round(t.pt/sum(t.bjg+t.pt+t.lh+t.yx)* 100 ,2),'%') as bl_pt
,concat(round(t.lh/sum(t.bjg+t.pt+t.lh+t.yx)* 100 ,2),'%') as bl_lh
,concat(round(t.yx/sum(t.bjg+t.pt+t.lh+t.yx)* 100 ,2),'%') as bl_yx from (
select cid
,sum(case when a.score < 60 then 1 else 0 end) as 'bjg'
,sum(case when a.score > 60 and a.score <= 70 then 1 else 0 end) as 'pt'
,sum(case when a.score > 70 and a.score <= 80 then 1 else 0 end) as 'lh'
,sum(case when a.score > 80 then 1 else 0 end) as 'yx'
from SC a group by a.cid ) t
LEFT JOIN Course c on t.cid =c.cid
GROUP BY t.cid
; #直接计算
SELECT a.Cid
       ,a.cname
       ,SUM(CASE WHEN b.score<=100 AND b.score>85 THEN 1 ELSE 0 END) AS 'yx'
       ,SUM(CASE WHEN b.score<=85 AND b.score>70 THEN 1 ELSE 0 END) AS 'lh'
       ,SUM(CASE WHEN b.score<=70 AND b.score>60 THEN 1 ELSE 0 END) AS 'jg'
       ,SUM(CASE WHEN b.score<=60 AND b.score>0 THEN 1 ELSE 0 END) AS 'bjg'   
       ,SUM(CASE WHEN b.score<=100 AND b.score>85 THEN 1 ELSE 0 END)/COUNT(1) AS 'yx%'
       ,SUM(CASE WHEN b.score<=85 AND b.score>70 THEN 1 ELSE 0 END)/COUNT(1) AS 'lh%'
       ,SUM(CASE WHEN b.score<=70 AND b.score>60 THEN 1 ELSE 0 END)/COUNT(1) AS 'jg%'
       ,SUM(CASE WHEN b.score<=60 AND b.score>0 THEN 1 ELSE 0 END)/COUNT(1) AS 'bjg%'
FROM Course a
INNER JOIN SC b
ON a.Cid=b.Cid
GROUP BY 1,2 ;

    24、查询学生平均成绩及其名次

set @mc := 0;
select t.* ,@mc := @mc + 1 as '名次' from (
select c.Sid,c.Sname,sum(b.score)/3 as pjf from Course a LEFT JOIN SC b on a.Cid = b.Cid
RIGHT JOIN Student c on b.Sid =c.Sid group by b.Sid ORDER BY pjf desc ) t ; #方法2
SELECT a.*
,COUNT(b.Sid)+1
FROM (
SELECT a.*,AVG(CASE WHEN b.SCore IS NULL THEN 0 ELSE b.SCore END) AS aSCore
FROM Student a
LEFT JOIN SC b
ON a.Sid=b.Sid
GROUP BY 1,2,3,4
)a
LEFT JOIN(
SELECT a.*,AVG(CASE WHEN b.SCore IS NULL THEN 0 ELSE b.SCore END) AS aSCore
FROM Student a
LEFT JOIN SC b
ON a.Sid=b.Sid
GROUP BY 1,2,3,4
)b
ON a.aSCore<b.aSCore
GROUP BY 1,2,3,4,5 ;

    25、查询各科成绩前三名的记录

##题22 方法的同种类型

SELECT
c.Sname,
d.cid,
d.score,
d.tp
FROM
Student c
INNER JOIN (
SELECT
a.*, count(b.Cid) + 1 AS tp
FROM
SC a
LEFT JOIN SC b ON a.Cid = b.Cid
AND a.score < b.score
GROUP BY
a.Sid,
a.Cid
HAVING
count(b.Cid) + 1 <4
) d ON c.Sid = d.sid order by d.cid; SELECT a.*,COUNT(b.cid)+1 AS ascore
FROM SC a
LEFT JOIN SC b
ON a.cid=b.cid AND a.score<b.score
GROUP BY 1,2,3
HAVING ascore<=3
ORDER BY a.cid,ascore ;

    26查询每门课程被选修的学生数

SELECT a.* ,COUNT(b.sid)
FROM Course a
LEFT JOIN SC b
ON a.cid=b.cid
GROUP BY 1,2,3 ;

    27、查询出只有两门课程的全部学生的学号和姓名

SELECT
a.*
FROM
Student a
LEFT JOIN SC b ON a.Sid = b.Sid
GROUP BY
b.Sid
HAVING
count(b.cid) = 2;

    28、查询男生、女生人数

SELECT
a.Ssex,
count(a.Sid)
FROM
Student a
GROUP BY
a.Ssex;  
#29、查询名字中含有"风"字的学生信息
select * from Student a where a.Sname like '%风%';
#30、查询同名同性学生名单,并统计同名人数
select * from Student a group by a.Sname HAVING count(a.Sname)>1; #31、查询1990年出生的学生名单(注:Student表中Sage列的类型是datetime)
SELECT * FROM Student WHERE YEAR(sage)=1990 ;

    #32、查询每门课程的平均成绩,结果按平均成绩降序排列,平均成绩相同时,按课程编号

SELECT a.*,AVG(b.Score) as ore
FROM Course a
LEFT JOIN SC b
ON a.cid=b.cid
GROUP BY 1,2,3
ORDER BY ore DESC,a.cid ;

    33、查询平均成绩大于等于85的所有学生的学号、姓名和平均成绩

SELECT
a.Sid,
a.Sname,
sum(
CASE
WHEN b.score IS NULL THEN
0
ELSE
b.score
END
) / 3 AS avgsc
FROM
Student a
LEFT JOIN SC b ON a.Sid = b.Sid
GROUP BY
a.Sid
HAVING
avgsc > 85;

    34、查询课程名称为"数学",且分数低于60的学生姓名和分数

SELECT
a.*, b.score
FROM
Student a
LEFT JOIN SC b ON a.Sid = b.sid
LEFT JOIN Course c ON b.Cid = c.Cid
WHERE
b.score < 60
AND c.Cname = '数学' or c.Cname is null;

    35、查询所有学生的课程及分数情况;

SELECT
c.cname,a.*,b.score
FROM
Student a
LEFT JOIN SC b ON a.Sid = b.Sid
LEFT JOIN Course c on b.Cid = c.Cid
ORDER BY
c.cname,
a.Sid,
b.Cid;

    36、查询任何一门课程成绩在70分以上的姓名、课程名称和分数;

SELECT DISTINCT
b.Sname,
c.Cname,
a.score
FROM
Student b
LEFT JOIN SC a ON a.Sid = b.Sid
LEFT JOIN Course c ON c.Cid = a.Cid
WHERE
a.score > 70;

    #37 不及格的

SELECT DISTINCT
b.Sname,
c.Cname,
a.score
FROM
Student b
LEFT JOIN SC a ON a.Sid = b.Sid
LEFT JOIN Course c ON c.Cid = a.Cid
WHERE
a.score < 60 or a.score is null;

    #38查询课程编号为01且课程成绩在80分以上的学生的学号和姓名;

SELECT DISTINCT
b.Sname,
c.Cname,
a.score
FROM
Student b
LEFT JOIN SC a ON a.Sid = b.Sid
LEFT JOIN Course c ON c.Cid = a.Cid
WHERE
a.score > 80 and a.Cid='01';

    39、求每门课程的学生人数

SELECT
a.Cid,
count(a.Sid)
FROM
SC a
GROUP BY
a.Cid;

    40、查询选修"张三"老师所授课程的学生中,成绩最高的学生信息及其成绩

#方法1:排序获取结果
SELECT
*
FROM
Student a
LEFT JOIN SC b ON a.Sid = b.Sid
LEFT JOIN Course c ON b.Cid = c.Cid
LEFT JOIN Teacher d ON c.Tid = d.Tid
WHERE
d.Tname = '张三'
ORDER BY
b.score DESC
LIMIT 1; #方法2: 通过内连接最大值匹配
SELECT a.*,b.score
FROM Student a
INNER JOIN SC b
ON a.Sid=b.Sid
INNER JOIN(
SELECT c.Cid ,MAX(c.score) AS maxscore FROM Teacher a
INNER JOIN Course b
ON a.Tid=b.Tid
INNER JOIN SC c
ON b.Cid=c.Cid
WHERE a.Tname='张三'
GROUP BY c.Cid )c
ON b.Cid=c.Cid AND b.score=c.maxscore ;

    41、查询不同课程成绩相同的学生的学生编号、课程编号、学生成绩

SELECT DISTINCT
c.Sname,
c.Sid,
a.Cid,
d.cname,
a.score
FROM
Student c
LEFT JOIN SC a ON a.Sid = c.Sid
INNER JOIN SC b ON a.sid = b.Sid
LEFT JOIN Course d ON a.Cid = d.Cid
WHERE
a.Cid != b.Cid
AND a.score = b.score; #方法2
SELECT a.sid
,a.cid
,a.SCore
FROM SC a
INNER JOIN (
SELECT a.SCore
,b.sid
,COUNT(1)
FROM SC a
INNER JOIN Student b
ON a.sid=b.sid
GROUP BY a.SCore,b.sid
HAVING COUNT(1)>1
)b
ON a.sid=b.sid AND a.SCore=b.SCore ;

    42、查询每门功成绩最好的前两名 ,同22

SELECT
c.Sname,
d.cid,
d.score,
d.tp
FROM
Student c
INNER JOIN (
SELECT
a.*, count(b.Cid) + 1 AS tp
FROM
SC a
LEFT JOIN SC b ON a.Cid = b.Cid
AND a.score < b.score
GROUP BY
a.Sid,
a.Cid
HAVING
count(b.Cid) + 1 <3
) d ON c.Sid = d.sid order by d.cid;

    43、统计每门课程的学生选修人数(超过5人的课程才统计)。要求输出课程号和选修人数,查询结果按人数降序排列,若人数相同,按课程号升序排列

SELECT
a.Cid,
a.Cname,
count(b.Sid)
FROM
Course a
LEFT JOIN SC b ON a.Cid = b.Cid
GROUP BY
b.Cid
HAVING
count(b.Sid) > 5
ORDER BY
count(b.Sid) DESC,
b.Cid;

    

    

#方法3::通过组装cid字段比对进行筛选#重点:GROUP_CONCAT(cid ORDER BY cid) 函数的用法#1.需要配合group by 子句使用#2.内部字段的排序的用法
SELECT DISTINCTt.*FROMStudent tINNER JOIN SC t1 ON t.Sid = t1.SidWHEREt.Sid IN (SELECTt3.SidFROM(SELECTsid,GROUP_CONCAT(cid ORDER BY cid) AS cid_listFROMSC aWHEREa.sid = '01'GROUP BYsid) t2RIGHT JOIN (SELECTsid,GROUP_CONCAT(cid ORDER BY cid) AS cid_listFROMSC aGROUP BYsid) t3 ON t2.cid_list = t3.cid_listWHEREt2.sid IS NOT NULL);

MySQL--关于MySQL的那些练习题的更多相关文章

  1. MySQL: Table 'mysql.plugin' doesn't exist的解决

    安装解压版MySQL以后,不能启动,日志里面出现了这个错误: MySQL: Table 'mysql.plugin' doesn't exist 这是因为mysql服务启动时候找不到内置数据库&quo ...

  2. MySql 及 MySql WorkBench使用大全

    Mysql安装步骤 1. 下载MySQL Community Server 5.6.13 2. 解压MySQL压缩包 将以下载的MySQL压缩包解压到自定义目录下,我的解压目录是: "D:\ ...

  3. mac使用终端运行mysql,mysql终端,mysql mac,mysql目录,mysql路径

    首先去官网下载: http://www.mysql.com/downloads/ 我下载了5.6.11的dmg然后安装,安装完成之后..如果要用终端去玩SQL.那么一开始要输入很长的:/usr/loc ...

  4. yum安装mysql和mysql源,配置mysql

    申明,不要用root安装 1. 下载mysql的repo源 $ wget http://repo.mysql.com/mysql-community-release-el7-5.noarch.rpm ...

  5. MySQL服务 - MySQL列类型、SQL模式、数据字典

    MySQL列类型的作用: 列类型可以简单理解为用来对用户往列种存储数据时做某种范围"限定",它可以定义数据的有效值(字符.数字等).所能占据的最大存储空间.字符长度(定长或变长). ...

  6. OS 系统下安装MySql 配置MySql环境变量

    学习Hive需要,闲话不说 本文的内容: 下载Mysql for Mac 下载Mysql Workbench 安装 Mysql 和 Mysql Workbench 配置Mysql在OS 系统上的环境变 ...

  7. 详解 Spotlight on MySQL监控MySQL服务器

    前一章详解了Spotlight on Unix 监控Linux服务器 ,今天再来看看Spotlight on MySQL怎么监控MySQL服务器. 注:http://www.cnblogs.com/J ...

  8. Navicat for MySQL连接MYSQL出错,错误代码1045的解决方法

    Navicat for MySQL连接MYSQL

  9. [转]MySQL: Starting MySQL….. ERROR! The server quit without updating PID file

    转自: http://icesquare.com/wordpress/mysql-starting-mysql-error-the-server-quit-without-updating-pid-f ...

  10. 【转】【MySQL】mysql 通过bin-log恢复数据方法详解

    mysql中bin-log在mysql默认状态下是没有打开的,我们要先打开mysql 开启bin-log功能,然后再通过备份的bin-log进行数据库恢复了. 具体的操作是通过mysqlbinlog这 ...

随机推荐

  1. C学习笔记-枚举

    枚举定义 可以使用枚举(enumerated type)声明代表整数常量的符号名称,关键字enum创建一个新的枚举类型 实际上,enum常量是int类型的 枚举的本质就是int型的常量 enum sp ...

  2. luoguP1886 滑动窗口(单调队列模板题)

    题目链接:https://www.luogu.org/problem/P1886#submit 题意:给定n个数,求大小为k的滑动窗口中最小值和最大值. 思路:单调队列模板题. AC代码: #incl ...

  3. [Python3] 034 函数式编程 匿名函数

    目录 函数式编程 Functional Programming 1. 简介 2. 函数 3. 匿名函数 3.1 lambda 表达式也称"匿名函数" 3.2 lambda 表达式的 ...

  4. java暂停线程

    暂停线程 本节介绍两个被废弃的用于线程暂停和恢复的方法suspend().resume().主要探究废弃原因,强调线程的安全性.主要有两个原因 原因1: suspend().resume()使用不当, ...

  5. 多模块环境下修改包名Rename directory与Rename package

    选中包名->右键Refactor->Rename,如果会弹出的警告框让选择Rename directory和Rename package时,若选择Rename directory,则是只修 ...

  6. Ruby学习中(首探数组, 注释, 运算符, 三元表达式, 字符串)

    一. 数组 1.定义一个数组 games = ["英雄联盟", "绝地求生", "钢铁雄心"] puts games 2.数组的循环 gam ...

  7. vue-router动态路由设置参数可选

    在日常工作中,我们需要将匹配到的所有路由,映射到一个组件上. 如下代码想要达到的效果: 不传page和id,则映射到user默认list页面 传page和id,根据page不同,显示不同的页面 问题 ...

  8. Date对象中的方法

    特殊说明:设置时间的方法,虽然W3C说明传参的范围,在开发过程中,传入的参数不在该范围也是可以的.例如: var t = new Date(), d = t.getDate(); //当天时间往前推2 ...

  9. vscode常用插件列表

    vscode插件 备注 Markdown PDF 把markdown文件转换成别的文件 Markdown TOC markdown文件目录生成 PHP Debug PHP调试 PHP Intenlli ...

  10. 取出input内的空格

    onkeyup='this.value=this.value.replace(' ','')'