MySQL-- comprehensive summary of two classic topics (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: find all the courses the students grade point average ranking in the information 2-4 (finale)

        Analysis: The title is more difficult, for example, the title for the finale, there is no good foundation, then very difficult. The difficulty is that demand is 2-4 students, not the first or the last one, but also requires an average ranking points , but it is bound to use the group by this information is lost, then the method of two-dimensional table into our clever use of the use of tables in ensuring the rank order of the merger process, the code is:

select stu.s_id,s_name,s_sex,s_age,s_bothday from   # 法一
  (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 limit 1,3)aa 
  left join stu on aa.s_id=stu.s_id;

select stu.s_id,s_name,s_sex,s_age,s_bothday from    # 法二 博主的第二种方法,建议基础较好的朋友看,比较绕
    (select s_id,round(avg(score),2)平均分 from scores group by s_id order by 平均分 desc limit 1,3)a 
    left join stu on a.s_id=stu.s_id;
# 以上两种方法答案均为
001	李华	男	23	1996-8-16
002	王二	男	24	1997-3-16
003	赵敏	女	23	1990-5-26
Topic Two: Query different teachers teach different courses Average high to low display (finale)
select t_name,a1.c_id,avg(score)平均分 from   # 法一
  (select c_id,score from scores)a1 left join
  (select c_id,teacher.t_id,t_name from coure left join teacher
  on coure.t_id=teacher.t_id)a2 on a1.c_id=a2.c_id group by 
  t_name,c_id order by 平均分 desc;
# 法二  适合用于一位老师教一门课程
select t_name,scores.c_id,avg(score)平均分 from scores left join 
  coure on scores.c_id=coure.c_id left join teacher on 
  coure.t_id=teacher.t_id group by scores.c_id order by 平均分 desc;
# 结果为:
汪院长	01	113.75
崔博	02	107.1
邓博	03	93.4499994913737
Topic three: check out the only two courses of all students in school number and name
select stu.s_id,s_name from stu left join scores on 
   stu.s_id=scores.s_id group by scores.s_id having count(*)=2;
# 结果:
002	王二
004	张莹莹
006	马云
Topic Four: query did not learn the full information of the students for all courses

       Analysis: The whole school students is no less his course number, so we can just find all the courses are full and then separately for each course students will be able to compare. Code:

select stu.* from stu left join scores on stu.s_id=scores.s_id # 法一 
  group by scores.s_id having count(*)< (select count(*)课程数 from coure);
 
select * from stu left join scores on stu.s_id=scores.s_id group by 
  scores.s_id having group_concat(scores.c_id order by scores.c_id)!=
  (select group_concat(c_id order by c_id) from coure); #法二 group_concat
# 结果:
002	王二	男	24	1997-3-16
004	张莹莹	女	22	1995-2-16
006	马云	男	28	1993-12-16
Information inquiry never learned, "Wang Yuanzhang" teachers to teach the students: five topics
select stu.* from stu where s_id not in  #法一
  (select stu.s_id 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 t_name="汪院长");
 
select stu.* from stu where s_id not in  # 法二
(select s_id from scores where c_id =
(select c_id from coure left join teacher on coure.t_id=teacher.t_id where t_name="汪院长")) group by stu.s_id;
# 结果
004	张莹莹	女	22	1995-2-16
006	马云	男	28	1993-12-16
Topic Six: query has at least two courses and student number is "002," the students learn the same curriculum students information (finale)

        The greater the difficulty of the subject, is the subject of my own set, or is at least two courses have the same 002, the same or more than two courses. The first to use the curriculum 002 group_concat find and learn, and then match it to other students, if found in 002 other students there, then that is the answer, can really achieve it?

select stu.* from stu left join scores on stu.s_id=scores.s_id group 
  by stu.s_id having group_concat(c_id order by c_id) in (select 
  group_concat(c_id) from scores where s_id = 002); # 此方法不对
# 答案是NO!这样是做不出来的,因为group_concat返回的结果是以逗号
# 分隔的字符串,如果先找到属于002的,它就是01,03,万一其他判断是01,02,03或者其他情况,就没法
# 判断,所有要单独将找到的01,03用括号列举出来正确答案为:

select c_id from scores where s_id = 002; # 先找到002的课程
select stu.* from stu where s_id in (select s_id from scores group by 
  s_id having group_concat(c_id order by c_id) in (01,03)) and s_id!=002; # 法一 子查询

select stu.* from stu left join scores on stu.s_id=scores.s_id where c_id in # 法二 多表连接
(select c_id from scores where s_id = 002) group by stu.s_id  having count(*)>=2 and stu.s_id!=002;
# 结果
001	李华	男	23	1996-8-16
003	赵敏	女	23	1990-5-26
005	朱亚军	男	25	1999-8-16
Topic Seven: Query learned numbered "01" and I have learned a number "02" of the course the students information
select stu.* from stu left join scores on stu.s_id=scores.s_id where c_id in (01,02)
  group by stu.s_id having count(*)>=2;
# 结果
001	李华	男	23	1996-8-16
003	赵敏	女	23	1990-5-26
005	朱亚军	男	25	1999-8-16
Topic Eight: the query "01" class than the "02" high-achieving students in the course of information and points (finale)

        The difficulty of the subject is relatively high, because there are scores c_id in the same column can not be compared, then get into a two-dimensional table to compare screening:

select stu.s_id,s_name,s_sex,s_bothday,课程1 from 
  (select s_id,
  round(sum(c_id='01')*score,2) 课程1,
  round(sum(c_id='02')*score,2) 课程2,
  round(sum(c_id='03')*score,2) 课程3
  from scores group by s_id)a1 left join stu on 
  a1.s_id=stu.s_id where 课程1>课程2 and 课程2!=0;
# 结果:可以看到并没有结果,说明不存在01课程比02课程分数高的同学,
# 所以02可能是语文,而01可能是数学或者英语~
Topic Nine: Query learned numbered "01" but did not learn of the course students numbered information of "02"
select s_id,group_concat(c_id order by c_id)课程号 from scores 
  group by s_id having  '02' not in (课程号) and '01' in (课程号);
  # 等一下,看看我这个代码为什么查不出来,困扰我很久了。该方法不对!!
# 正确的代码
select * from stu where s_id in
  (select s_id from
  (select s_id,
  sum((c_id='01')*score) '一',
  sum((c_id='02')*score) '二',
  sum((c_id='03')*score) '三'
  from scores group by s_id having!=0 and=0)a1);
# 结果
002	王二	男	24	1997-3-16
Topic Ten: Query Performance Name any one course of 100 points or more, the course name and scores
select stu.s_name,c_name,score 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 score>100 order by stu.s_id;
# 结果为:
朱亚军	统计学基础	140
朱亚军	Python基础	100.6
朱亚军	数据库原理	120
李华	数据库原理	135
李华	统计学基础	125.5
王二	Python基础	102
赵敏	数据库原理	110

3, the end of the paper eggs - easy moment

        And to everyone's favorite part of the egg, and today still bring my good friend Winnie the story. Is the sophomore, bear it went to a dairy farm to find a job is milking, bright brother and I heard that he went to see his first day of work, the result was the boss gave him a bucket and a bench so that he to milk the milking shed, he happily kicked away. Came back sweating, to see he was covered with spilled milk, but that stool legs are broken, the boss asked him: "? How, quite hard to do it live," he Distressed replied: "squeeze pour milk is not difficult, the difficulty is to make cattle stand stool up ... "and then I was anxious eyes bright brother said:" cow standing on a stool how you can squeeze your fucking half a brain you should let the cows?? sitting on the bench top, so you go operation Well ... "so now you know why I can not find very high-paying job, right?

       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/102315066