MySQL-- comprehensive summary of a classic title (Advanced) - recommended first looked at before, a higher degree of difficulty


      For more information about the database, please add attention yo ~ ~. For bloggers please contact Gabor main private letter or contact:
      QQ: 3327908431
      micro letter: ZDSL1542334210

        Introduction: Today, MySQL will bring you comprehensive training title, this section is based on the contents of each section before, so the foundation is weak partner recommendations before the first article looked at again continued reading the article ---- Good luck !

1, table creation

create table stu(s_id varchar(5),
   s_name varchar(5),
   s_sex varchar(1),
   s_age int(3),s_day date);

insert into stu values
("001","李华","男",23,'1996-8-16'),
("002","王二","男",24,'1997-3-16'),
("003","赵敏","女",23,'1990-5-26'),
("004","张莹莹","女",22,'1995-2-16'),
("005","朱亚军","男",25,'1999-8-16'),
("006","马云","男",28,'1993-12-16');

create table scores (s_id varchar(5),
    c_id varchar(3),score float);
    
insert into scores values 
("001","01",135),
("005","01",120),
("003","01",110),
("002","01",90),
("005","02",140),
("001","02",125.5),
("004","02",100),
("006","02",90),
("002","03",102),
("005","03",100.6),
("001","03",100),
("003","03",95.6),
("004","03",83),
("003","02",80),
("006","03",79.5);

create table coure(
c_id varchar(2),t_id char(1),c_name varchar(10));

insert into coure values 
("01","3","数据库原理"),
("02","2","统计学基础"),
("03","1","Python基础");

create table teacher(
 t_id char(1),t_name varchar(5));
 
insert into teacher values
  ("1","邓博"),
  ("2","崔博"),
  ("3","汪院长");

2, section title

Topic one: Course query name "python basis" and scores below 90 student names and scores

Analysis: '.', Will need to be connected to form three clear meaning of the questions, and the conditions for the course name "python basis" and scores below 90, here, using the 'table' + + 'field' then said its inquiry field of the table for which the code is:

select stu.s_name,score from stu left join scores on 
   stu.s_id=scores.s_id left join coure on scores.c_id=coure.c_id
   where score<90 and c_name="python基础";
# 结果
张莹莹	83
马云	79.5
Topic Two: No. 01 about courses and course grades at 90 points or more students in school, name and scores
select stu.s_id,stu.s_name,score from stu left join scores on 
    stu.s_id=scores.s_id where c_id=01 and score>=90; # 法一 表格连接

select stu.s_id,s_name from stu where s_id in
    (select s_id from scores where c_id=01 and score>90);# 法二 子查询 该方法没有将分数查询下来
# 结果
001	李华	135
002	王二	90
003	赵敏	110
005	朱亚军	120
Topic three: query all students student number, student name, total enrollment, a total score of all the courses
select stu.s_id,stu.s_name,count(*),sum(score) from stu 
    left join scores on stu.s_id=scores.s_id group by s_id;
# 结果
001	李华	3	360.5
002	王二	2	192
003	赵敏	3	285.5999984741211
004	张莹莹	2	183
005	朱亚军	3	360.5999984741211
006	马云	2	169.5
Topic Four: inquiry average score of 100 points or greater number of students and the student's name and student grade point average
select stu.s_id,s_name,avg(score) from stu left join scores on 
    stu.s_id=scores.s_id  group by s_id having avg(score)>=100;
# 结果
001	李华	120.16666666666667
005	朱亚军	120.1999994913737
Topic Five: Query average score greater than or equal to 100 students of all school number, name and achievements
select stu.s_id,s_name,score from stu left join scores on 
   stu.s_id=scores.s_id group by stu.s_id having avg(score)>=100;

select s_id,s_name from stu where s_id in# 法二 子查询 无分数
    (select s_id from scores group by s_id having avg(score)>=100);
# 结果
001	李华	135
005	朱亚军	120
Topic Six: Query failed two or more courses and students of the school number, name and grade point average, respectively, to 90,100 points as a pass line
select stu.s_id,s_name,avg(score) from stu left join scores on  
    stu.s_id=scores.s_id where score<90 group by s_id having count(*)>=2;
#很明显没有这样的学生,我们以100分看看
select stu.s_id,s_name,avg(score) from stu left join scores on 
    stu.s_id=scores.s_id where score<100 group by s_id having count(*)>=2;

select stu.s_id,s_name from stu where s_id in   #子查询
 (select s_id from scores where score<100 group by s_id having count(*)>=2);
# 结果为:
003	赵敏	87.79999923706055
006	马云	84.75
Topic Seven: retrieve student information "01" course score less than 100, in descending order by score
select * from stu left join scores on stu.s_id=scores.s_id
    where score<100 and c_id=01 order by score desc;
# 答案
002	王二	男	24	1997-3-16	002	01	90
Topic Eight: Query learned information "Wang Yuanzhang" of teachers to teach students
select * from stu left join scores on stu.s_id=scores.s_id  # 多表连接
   left join coure on scores.c_id=coure.c_id left join teacher on 
   coure.t_id=teacher.t_id where teacher.t_name="汪院长" group by stu.s_id;
# 结果
001	李华	男	23	1996-8-16	001	01	135	01	3	数据库原理	3	汪院长
002	王二	男	24	1997-3-16	002	01	90	01	3	数据库原理	3	汪院长
003	赵敏	女	23	1990-5-26	003	01	110	01	3	数据库原理	3	汪院长
005	朱亚军	男	25	1999-8-16	005	01	120	01	3	数据库原理	3	汪院长

select * from stu where s_id in  # 子查询
   (select s_id from scores where c_id in 
   (select c_id from coure where t_id in 
   (select t_id from teacher where t_name="汪院长")));
# 答案
001	李华	男	23	1996-8-16
002	王二	男	24	1997-3-16
003	赵敏	女	23	1990-5-26
005	朱亚军	男	25	1999-8-16
Topic nine: the average score from high to low scores and grade point average display all courses for all students (finale)

        Analysis: The title is more difficult, it is recommended carefully taste. Questions asked students to display curriculum, grades, grade point average, the hardest part is also required to display the average score for each subject and each student scores, the average score for each student seeking need to be grouped, but the grouping information is lost, information about all courses how come? Zhu brother see how to solve: First, the group obtained average student number and corresponding fitted with a table, then its left connected students score table, and then sort it is the answer, code:

select a.s_id,平均分,c_id,score from   #  法一 子查询
    (select s_id,round(avg(score),2)平均分 from scores group by s_id)a
    left join scores on a.s_id=scores.s_id order by 平均分 desc;
# 结果为:
s_id 平均分 c_id score
005	120.20	01	120
005	120.20	03	100.6
005	120.20	02	140
001	120.17	01	135
001	120.17	03	100
001	120.17	02	125.5
002	96.00	03	102
002	96.00	01	90
003	95.20	02	80
003	95.20	01	110
003	95.20	03	95.6
004	91.50	03	83
004	91.50	02	100
006	84.75	02	90
006	84.75	03	79.5
# 该结果看起来很让人不爽,于是可以将其转换为二维表,以学号与课程号做行标题和列标题
select s_id,
  round(sum(c_id='01')*score,2) '01',    #转为二维表的方式
  round(sum(c_id='02')*score,2) '02',
  round(sum(c_id='03')*score,2) '03',
  round(avg(score),2) 平均分
  from scores group by s_id order by 平均分 desc;
# 结果为: ps 这样看是不是很爽呢?代码也很简单
#s_id  01     02       03   平均分 
005	120.00	120.00	120.00	120.20
001	135.00	135.00	135.00	120.17
002	90.00	0.00	90.00	96.00
003	110.00	110.00	110.00	95.20
004	0.00	100.00	100.00	91.50
006	0.00	90.00	90.00	84.75
Topic Ten: The average score of the top three students seeking information - sub-queries
select stu.* from (select s_id,round(avg(score),2) 平均成绩 from 
  scores group by s_id   order by 平均成绩 desc limit 3)aa ,stu where 
  stu.s_id=aa.s_id;
# 结果
001	李华	男	23	1996-8-16
002	王二	男	24	1997-3-16
005	朱亚军	男	25	1999-8-16

3, the end of the paper eggs - easy moment

        And to everyone's favorite easy moment, and many of my friends asked me recently why the Cubs, but I do not have the nerve to say, I would simply bring his girlfriend is the story of my sister-in-law, which is not the day before yesterday Well, her girlfriend students attend her engagement party, when she was in order to show that he has been married to hand out, but there is not even a classmate noticed, made her indignant. In the afternoon we sat chatting, she suddenly stood up and said loudly: "Oh, really hot here Yeah, I think I'd come off the ring, the heat death of my finger ..." See here, I believe we all understand Why did he two come together, ha ha ...

       Today to end here yo // each article has the end the egg - relaxed moment yo ~ plus interest to learn more about MySQL knowledge! Thank you for watching, I was Jetuser-data

Links: [https://blog.csdn.net/L1542334210]
CSND: L1542334210
Here Insert Picture Description
I wish you all success! Family fun!

Published 29 original articles · won praise 53 · views 30000 +

Guess you like

Origin blog.csdn.net/L1542334210/article/details/102253427