首页 > 其他分享 >2022-8-17 第一组 (≥▽≤) 学习笔记

2022-8-17 第一组 (≥▽≤) 学习笔记

时间:2022-08-17 18:22:34浏览次数:63  
标签:insert 17 第一组 score VALUES 2022 scores sc id

目录

1.DQL查询语言

1.1 子查询(自连接)

按照结果集的行列数不同,子查询可以分为以下几类:

  • 标量子查询

    • 结果集只有一行一列(单行子查询)
  • 列子查询

    • 结果集有一列多行
  • 行子查询

    • 结果集有一行多列
  • 表子查询

    • 结果集是多行多列

where型子查询,如果where列=(内层sql),则内层的sql返回的必须是单行单列,单个值

where型子查询,如果where(列1,列2)=(内层sql),则内层的sql返回的可以是单列,可以是多行

  • 经验分享:
    • 分析需求
    • 拆步骤
    • 分步写sql
    • 整合拼装sql
-- 查询每个老师的代课数
SELECT t.id, t.NAME,( SELECT count(*) FROM course c WHERE c.id = t.id ) AS 代课的数量 
FROM
	teacher t;
----------------------------------------------------------------------------
SELECT
	t.id,
	t.NAME,
	count(*) '代课的数量' 
FROM
	teacher t
	LEFT JOIN course c ON c.t_id = t.id 
GROUP BY
	t.id,
	t.NAME;
SELECT
	* 
FROM
	teacher t 
WHERE
	EXISTS ( SELECT * FROM course c WHERE c.t_id = t.id );
----------------------------------------------------------------------------SELECT
	t.*,
	c.`name` 
FROM
	teacher t
	INNER JOIN course c ON t.id = c.t_id;	

总结:如果一个需求可以不用子查询,尽量不使用。

sql可读性太低。

需求

建表

DROP TABLE IF EXISTS `student`;

CREATE TABLE `student`(
	`id` INT(10) PRIMARY KEY ,
	`name` VARCHAR(10) ,
	`age` INT(10) NOT NULL,
	`gender` VARCHAR(2)
)

DROP TABLE if EXISTS `course`

CREATE TABLE `course`(
	`id` INT(10) PRIMARY key,
	`name` VARCHAR(10) ,
	`t_id` INT(10)
)

DROP TABLE if EXISTS `teacher`

CREATE TABLE `teacher`(
	`id` INT(10) PRIMARY KEY,
	`name` VARCHAR(10)
)

DROP TABLE if EXISTS `score`;

CREATE TABLE `score`(
	`s_id` INT(10),
	`score` INT(10),
	`c_id` INT(10),
	PRIMARY KEY(`s_id`,`c_id`)
)
SELECT DISTINCT
SELECT gender as 性别 FROM author AS `A`
SELECT * FROM study LIMIT 220,6;

插入数据

insert into  student (id,name,age,gender)VALUES(1,'小明',19,'男'),(2,'小红',19,'男'),(3,'小刚',24,'男'),(4,'小龙',11,'男'),(5,'小丽',18,'男'),(6,'小军',18,'女'),(7,'小航',16,'男'),(8,'小亮',23,'男'),(9,'小杰',22,'女'),(10,'小虎',21,'男');

insert into  course (id,name,t_id)VALUES(1,'数学',1),(2,'语文',2),(3,'c++',3),(4,'java',4),(5,'php',null);


insert into  teacher (id,name)VALUES(1,'Tom'),(2,'Jerry'),(3,'Tony'),(4,'Jack'),(5,'Rose');


insert into  scores (s_id,score,c_id)VALUES(1,80,1);
insert into  scores (s_id,score,c_id)VALUES(1,56,2);
insert into  scores (s_id,score,c_id)VALUES(1,95,3);
insert into  scores (s_id,score,c_id)VALUES(1,30,4);
insert into  scores (s_id,score,c_id)VALUES(1,76,5);

insert into  scores (s_id,score,c_id)VALUES(2,35,1);
insert into  scores (s_id,score,c_id)VALUES(2,86,2);
insert into  scores (s_id,score,c_id)VALUES(2,45,3);
insert into  scores (s_id,score,c_id)VALUES(2,94,4);
insert into  scores (s_id,score,c_id)VALUES(2,79,5);

insert into  scores (s_id,score,c_id)VALUES(3,65,2);
insert into  scores (s_id,score,c_id)VALUES(3,85,3);
insert into  scores (s_id,score,c_id)VALUES(3,37,4);
insert into  scores (s_id,score,c_id)VALUES(3,79,5);

insert into  scores (s_id,score,c_id)VALUES(4,66,1);
insert into  scores (s_id,score,c_id)VALUES(4,39,2);
insert into  scores (s_id,score,c_id)VALUES(4,85,3);

insert into  scores (s_id,score,c_id)VALUES(5,66,2);
insert into  scores (s_id,score,c_id)VALUES(5,89,3);
insert into  scores (s_id,score,c_id)VALUES(5,74,4);


insert into  scores (s_id,score,c_id)VALUES(6,80,1);
insert into  scores (s_id,score,c_id)VALUES(6,56,2);
insert into  scores (s_id,score,c_id)VALUES(6,95,3);
insert into  scores (s_id,score,c_id)VALUES(6,30,4);
insert into  scores (s_id,score,c_id)VALUES(6,76,5);

insert into  scores (s_id,score,c_id)VALUES(7,35,1);
insert into  scores (s_id,score,c_id)VALUES(7,86,2);
insert into  scores (s_id,score,c_id)VALUES(7,45,3);
insert into  scores (s_id,score,c_id)VALUES(7,94,4);
insert into  scores (s_id,score,c_id)VALUES(7,79,5);

insert into  scores (s_id,score,c_id)VALUES(8,65,2);
insert into  scores (s_id,score,c_id)VALUES(8,85,3);
insert into  scores (s_id,score,c_id)VALUES(8,37,4);
insert into  scores (s_id,score,c_id)VALUES(8,79,5);

insert into  scores (s_id,score,c_id)VALUES(9,66,1);
insert into  scores (s_id,score,c_id)VALUES(9,39,2);
insert into  scores (s_id,score,c_id)VALUES(9,85,3);
insert into  scores (s_id,score,c_id)VALUES(9,79,5);

insert into  scores (s_id,score,c_id)VALUES(10,66,2);
insert into  scores (s_id,score,c_id)VALUES(10,89,3);
insert into  scores (s_id,score,c_id)VALUES(10,74,4);
insert into  scores (s_id,score,c_id)VALUES(10,79,5);
 

image-20220817163947426

-- 11 查询老师的信息和他所带的科目的平均分
SELECT t.*,c.`name` 科目, AVG(sc.score)
FROM
teacher t
LEFT  JOIN course c 
ON t.id=c.t_id
LEFT JOIN scores sc 
ON sc.c_id=c.id
GROUP BY
c.`name`

-- 查询每科的平均成绩
SELECT AVG(sc.score),c.`name` FROM scores sc LEFT JOIN course c ON sc.c_id=c.id GROUP BY c.`name`
-- 12 .查询被"Tom"和"Jerry"教的课程的最高分和最低分
SELECT t.`name`,MAX(sc.score),min(sc.score)
FROM
teacher t
LEFT  JOIN course c 
ON t.id=c.t_id
LEFT JOIN scores sc 
ON sc.c_id=c.id
WHERE t.`name` IN('Tom','Jerry')
GROUP BY
c.`name`
 
-- 13 查询每个学生的最好成绩的科目名称(子查询)
SELECT stu.`name`,c.`name`,sc.score
FROM
student stu 
LEFT JOIN scores sc 
ON sc.s_id=stu.id
LEFT JOIN course c 
on c.id=sc.c_id
WHERE (sc.score,stu.`name`) IN(
	SELECT MAX(sc.score),stu.`name`FROM student stu  LEFT JOIN scores sc  ON sc.s_id=stu.id GROUP BY stu.`name`)
GROUP BY 
stu.`name`
-- 14  查询所有学生的课程及分数
SELECT stu.`name`,c.`name`,sc.score
FROM
student stu
LEFT JOIN scores sc
ON sc.s_id=stu.id
LEFT JOIN course c 
ON c.id=sc.c_id
-- 15 查询课程编号为1且课程成绩在60分以上的学生的学号和姓名(子查询)
SELECT stu.id,stu.`name`,sc.`score`
FROM student stu 
LEFT JOIN scores sc 
ON sc.s_id=stu.id
WHERE sc.c_id = 1 AND sc.score >60

-- -------------------------------------------------
-- 16 查询平均成绩大于等于85的所有学生学号、姓名和平均成绩
SELECT stu.id,stu.`name`,AVG(sc.score)
FROM student stu 
LEFT JOIN scores sc 
ON stu.id=sc.s_id
GROUP BY  
stu.`name`
HAVING
AVG(sc.score) > 70
-- 17 查询有不及格课程的学生信息
SELECT tt.*
FROM
(SELECT stu.*, MIN(sc.score) min
FROM student stu 
LEFT JOIN scores sc 
ON stu.id = sc.s_id
GROUP BY
stu.`name`)tt 
WHERE
tt.min<60
-- 
SELECT student.*,scores.score
FROM student
LEFT JOIN scores
ON scores.s_id=student.id
WHERE
scores.score<60
GROUP BY
student.`name`
-- 
SELECT student.*,MIN(scores.score)
FROM student
LEFT JOIN scores
ON scores.s_id=student.id
GROUP BY
student.`name`
HAVING
MIN(scores.score) <60

-- 18 查询每门课程有成绩的学生人数
SELECT c.`name`,COUNT(*)
FROM scores sc 
LEFT JOIN course c 
ON c.id=sc.c_id
GROUP BY
c_id
-- 19 查询每门课程的平均成绩,结果按照平均成绩降序排列,如果平均成绩相同,再按照课程编号升序排列
SELECT AVG(score)
FROM scores 
GROUP BY
s_id
ORDER BY AVG(score) DESC,c_id ASC;
-- 20 查询平均成绩大于60分的同学的学生编号和学生姓名和平均成绩
SELECT stu.id,stu.`name`,AVG(sc.score)
FROM student stu 
LEFT JOIN scores sc 
ON sc.s_id=stu.id
GROUP BY
stu.`name`
HAVING
AVG(sc.score)>60

-- 21 查询有且仅有一门课程成绩在80分以上的学生信息
SELECT stu.*
FROM student stu 
LEFT JOIN scores sc 
on sc.s_id= stu.id
WHERE sc.score > 80
GROUP BY stu.`name`
HAVING COUNT(sc.s_id) =1

-- 22  查询出只有三门课程的学生的学号和姓名
SELECT c.id 学号,c.`name` 姓名,COUNT(sc.s_id) 选中科目数
FROM scores sc 
LEFT JOIN student c 
on c.id =sc.s_id
GROUP BY
sc.s_id
HAVING COUNT(sc.s_id)=3
-- 23 查询有不及格课程的课程信息
SELECT c.*
FROM scores sc 
LEFT JOIN course c 
ON c.id =sc.c_id
GROUP BY
c.`name`
HAVING MIN(sc.score)<60

-- 24 .查询至少选择4门课程的学生信息
SELECT c.*,COUNT(sc.s_id) 选中科目数
FROM scores sc 
LEFT JOIN student c 
on c.id =sc.s_id
GROUP BY
sc.s_id
HAVING COUNT(sc.s_id)>=4
-- 25 .查询没有学全所有课程的同学的信息
SELECT c.*,COUNT(sc.s_id) 选中科目数
FROM scores sc 
LEFT JOIN student c 
on c.id =sc.s_id
GROUP BY
sc.s_id
HAVING COUNT(sc.s_id)<(SELECT COUNT(*) FROM course)

-- 26 查询选全所有课程的同学的信息
SELECT c.* 
FROM scores sc 
LEFT JOIN student c 
on c.id =sc.s_id
GROUP BY
sc.s_id
HAVING COUNT(sc.s_id)=(SELECT COUNT(*) FROM course)

-- 27  查询各学生都选了多少门课
SELECT c.*,COUNT(sc.s_id) 选中科目数
FROM scores sc 
LEFT JOIN student c 
on c.id =sc.s_id
GROUP BY
sc.s_id
-- 28 查询课程名称为"java",且分数低于60分的学生姓名和分数
SELECT stu.`name`,sc.score
FROM student stu 
LEFT JOIN scores sc 
on sc.s_id=stu.id
LEFT JOIN course c 
on c.id=sc.c_id
WHERE sc.score<60 AND c.`name`='java'
GROUP BY
stu.`name`
-- 29 查询学过"Tony"老师授课的同学的信息
 SELECT stu.*
FROM student stu 
LEFT JOIN scores sc 
on sc.s_id=stu.id
LEFT JOIN course c 
on c.id=sc.c_id
LEFT JOIN teacher t 
ON t.id =c.t_id
WHERE
t.`name` ='Tony'
GROUP BY
stu.`name`
-- 30 查询没学过"Tony"老师授课的学生信息
 SELECT *
 FROM student
WHERE id  NOT IN(
SELECT stu.id
FROM student stu 
LEFT JOIN scores sc 
on sc.s_id=stu.id
LEFT JOIN course c 
on c.id=sc.c_id
LEFT JOIN teacher t 
ON t.id =c.t_id
WHERE
t.`name` ='Tony'
GROUP BY
stu.`name`
)

日期格式

格式 描述
%a 缩写的星期名
%b 缩写月名
%c 月,数值
%D 带有英文前缀的月中的天
%d 月的天,数值(00-31)
%e 月的天,数值(0-31)
%f 微秒
%H 小时(00-23)
%h 小时(01-12)
%I 小时(01-12)
%i 分钟,数值(00-59)
%j 年的天(001-366)
%k 小时(0-23)
%l 小时(1-12)
%M 月名
%m 月,数值(00-12)
%p AM或PM
%r 时间,12-小时 (hh:mm:ss AM或PM)
%S 秒(00-59)
%s 秒(0-59)
%T 时间,24-小时(hh:mm:ss)
%U 周(00-53)星期日是一周的第一天
%u 周(00-53)星期一是一周的第一天
%W 星期名
%Y 年,2022
%y 年,22

标签:insert,17,第一组,score,VALUES,2022,scores,sc,id
From: https://www.cnblogs.com/gycddd/p/16595968.html

相关文章

  • fm足球经理Football Manager 2022 for mac(真实模拟游戏)中文版
    FootballManager2022formac是一款真实模拟足球比赛的游戏,在充满活力的足球世界中,扮演真正的经理人的角色,在这里需要通过您敏锐的洞察力并结合游戏机制,打造您的专属管理......
  • 查询信息系统项目管理师2022年上半年考试成绩,意外通过,给自己点个赞!
    原本准备的是2021年下半年的考试,因成都疫情影响,准考证都打印出来了,结果临时取消了。终于2022年上半年的考试得以进行,殊为不易。考试在5月28日,这次考试内容变化有些大,刚考......
  • 排版软件indesign for Mac(id 2022)中文版
    InDesign2022formac被称为行业领先的印刷和数字媒体布局和页面设计软件,以PDF格式快速共享内容和反馈。使用AdobeExperienceManager轻松管理生产。InDesign拥有......
  • [游记]暑假集训5-2022.8.17
    今天的题目都比较有思维量,嗯A.星际旅行考虑一下去掉那两条有向边,就是一个典型的欧拉回路然后问的就是能够生成的欧拉回路的个数考虑每次删掉两条边,有三种删除方法:$\q......
  • NOIP2022模拟赛二 By yzxoi 8.17
    Preface今天早上被公交车搞了,晚了30min才到……最后T1读入\(n\)的时候写%d了,喜提30pts(结果Rank竟然不变233)A.「NOIP2022模拟赛二ByyzxoiA」『Pale』/feat.初音ミ......
  • OpenYurt 邀你共赴 2022 EdgeX 中国挑战赛!
    2022年8月3日,由LFEdge主办的第三届EdgeX中国挑战赛正式开幕,阿里云作为本届大赛战略合作伙伴重磅亮相,希望通过其主导开源的云原生边缘计算智能平台OpenYurt与E......
  • 172 树套树 线段树套平衡树
    视频链接:LuoguP3380【模板】二逼平衡树(树套树)//需要开O2#include<iostream>#include<algorithm>usingnamespacestd;#defineN2000010#defineINF21474836......
  • CF1719B Mathematical Circus
     题意简述:对于给定的$n,k$,能否将$1,2,3,...,n$($n$为偶数),两两分组.求对于每个分组($x_i$,$y_i$),是否全部满足$4\mid(x_i+k)*y_i$,如果分组全部......
  • [AcWing 1117] 单词接龙
    DFS点击查看代码#include<bits/stdc++.h>usingnamespacestd;typedeflonglongLL;constintN=50+10;intn;stringword[N];intg[N][N];//g[i][......
  • 2022“杭电杯”中国大学生算法设计超级联赛(9)
    赛后总结:不太理解为什么都这么强,1008是一道欧拉函数变形,我用莫比乌斯反演推出了一样的式子,实际上两个1e7的数的质数集合的并最多只有12个,那么暴力按照式子2^12枚举每个质......