当前位置:网站首页>Mysql50 basic exercises
Mysql50 basic exercises
2022-04-23 04:37:00 【Jieyou grocery store Q】
data
-- Student
create table Student(sid varchar(10),sname varchar(10),sage datetime,ssex nvarchar(10));
insert into Student values('01' , ' Zhao Lei ' , '1990-01-01' , ' male ');
insert into Student values('02' , ' Qian Dian ' , '1990-12-21' , ' male ');
insert into Student values('03' , ' Sun Feng ' , '1990-05-20' , ' male ');
insert into Student values('04' , ' Li Yun ' , '1990-08-06' , ' male ');
insert into Student values('05' , ' Zhou Mei ' , '1991-12-01' , ' Woman ');
insert into Student values('06' , ' Wu Lan ' , '1992-03-01' , ' Woman ');
insert into Student values('07' , ' Zheng Zhu ' , '1989-07-01' , ' Woman ');
insert into Student values('08' , ' Wangju ' , '1990-01-20' , ' Woman ');
-- Course
create table Course(cid varchar(10),cname varchar(10),tid varchar(10));
insert into Course values('01' , ' Chinese language and literature ' , '02');
insert into Course values('02' , ' mathematics ' , '01');
insert into Course values('03' , ' English ' , '03');
-- teacher
create table Teacher(tid varchar(10),tname varchar(10));
insert into Teacher values('01' , ' Zhang San ');
insert into Teacher values('02' , ' Li Si ');
insert into Teacher values('03' , ' Wang Wu ');
-- fraction
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);
Student
teacher
Course
fraction
01
Inquire about "01" Course than "02" Information and course marks for students with high course grades
First query 01 Course
The query again 02
intersection
SELECT
*
FROM
(SELECT * FROM SC WHERE cid='01') a
JOIN
(SELECT * FROM SC WHERE cid='02') b on a.sid = b.sid
Add restrictions ,01 fraction > 02 fraction
Association query course information
SELECT
*
FROM
(SELECT * FROM SC WHERE cid='01') a
JOIN
(SELECT * FROM SC WHERE cid='02') b on a.sid = b.sid and a.score > b.score
JOIN
Course c on a.cid = c.cid
JOIN
Course d on b.cid = d.cid
Association query student information
SELECT
*
FROM
(SELECT * FROM SC WHERE cid='01') a
JOIN
(SELECT * FROM SC WHERE cid='02') b on a.sid = b.sid and a.score > b.score
JOIN
Course c on a.cid = c.cid
JOIN
Course d on b.cid = d.cid
JOIN
Student e on a.sid = e.sid
Summary
-- 01
SELECT
e.sid,
e.sname,
e.sage,
e.ssex,
a.cid AS cid01,
c.cname AS cname01,
a.score AS cscore01,
b.cid AS cid02,
d.cname AS cname02,
b.score AS cscore02
FROM
( SELECT * FROM SC WHERE cid = '01' ) a
JOIN ( SELECT * FROM SC WHERE cid = '02' ) b ON a.sid = b.sid AND a.score > b.score
JOIN Course c ON a.cid = c.cid
JOIN Course d ON b.cid = d.cid
JOIN Student e ON a.sid = e.sid
02
Query the existence of students' course selection " 01 “ The course may not exist ” 02 " Course situation ( If it does not exist, it will be displayed as null)
SELECT
*
FROM
( SELECT * FROM SC WHERE cid = '01' ) a
LEFT JOIN ( SELECT * FROM SC WHERE cid = '02' ) b ON a.sid = b.sid
03
Query average score is greater than or equal to 60 Student number, student name and average score of each student
Query average score first
SELECT
*,
AVG(score) avg
FROM sc
GROUP BY sc.sid
HAVING avg >= 60
More table Association queries
SELECT
a.sid, b.sname, a.avg
FROM
( SELECT *, AVG( score ) avg FROM sc GROUP BY sc.sid HAVING avg >= 60 ) a
JOIN Student b ON a.sid = b.sid
版权声明
本文为[Jieyou grocery store Q]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230406554765.html
边栏推荐
- PHP export excel table
- 【论文阅读】【3d目标检测】Voxel Transformer for 3D Object Detection
- The perfect combination of collaborative process and multi process
- zynq平台交叉编译器的安装
- Record your own dataset with d435i, run orbslam2 and build a dense point cloud
- [mapping program design] coordinate azimuth calculation artifact (version C)
- Logger and zap log Library in go language
- [paper reading] [3D target detection] point transformer
- 协程与多进程的完美结合
- TreeSet after class exercises
猜你喜欢
STM32上μC/Shell移植与应用
Why recommend you to study embedded
为什么推荐你学嵌入式
How to regulate intestinal flora? Introduction to common natural substances, probiotics and prebiotics
IDE Idea 自动编译 与 On Upate Action 、 On Frame Deactivation 的配置
A new method for evaluating the quality of metagenome assembly - magista
【Echart】echart 入门
test
Recursive call -- Enumeration of permutations
[paper reading] [3D target detection] point transformer
随机推荐
VHDL implementation of 32-bit binary to BCD code
上海航芯技术分享 | ACM32 MCU安全特性概述
Iron and intestinal flora
Apache Bench(ab 压力测试工具)的安装与使用
Common string processing functions in C language
Bridge between ischemic stroke and intestinal flora: short chain fatty acids
test
test
Effects of antibiotics on microbiome and human health
Cortex-M3寄存器组、汇编语言与C语言的接口介绍
shell wc (统计字符数量)的基本使用
在AWS控制台创建VPC(无图版)
229. 求众数 II
thymeleaf th:value 为null时报错问题
Qtspim manual - Chinese Translation
Set classic topics
STM32F4单片机ADC采样及ARM-DSP库的FFT
[BIM introduction practice] wall hierarchy and FAQ in Revit
Fusobacterium -- symbiotic bacteria, opportunistic bacteria, oncobacterium
【BIM+GIS】ArcGIS Pro2. 8 how to open Revit model, Bim and GIS integration?