当前位置:网站首页>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
边栏推荐
- STM32F4单片机ADC采样及ARM-DSP库的FFT
- MySQL 2013 lost connection to MySQL server during query
- 【Pytorch基础】torch.split()用法
- 递归调用--排列的穷举
- Brushless motor drive scheme based on Infineon MCU GTM module
- 【论文阅读】【3d目标检测】Voxel Transformer for 3D Object Detection
- Interaction of diet gut microbiota on cardiovascular disease
- 【Echart】echart 入门
- C语言常用字符串处理函数
- Use recyclerview to realize left-right side-by-side classification selection
猜你喜欢

Express middleware ② (classification of Middleware)

STM32 upper μ C / shell transplantation and Application

补:注解(Annotation)

无线键盘全国产化电子元件推荐方案
![[echart] Introduction to echart](/img/40/e057f4ac07754fe6f3500f3dc72293.jpg)
[echart] Introduction to echart

C语言常用字符串处理函数

【BIM+GIS】ArcGIS Pro2. 8 how to open Revit model, Bim and GIS integration?

Common string processing functions in C language

Installation and use of Apache bench (AB pressure test tool)

Supplément: annotation
随机推荐
TreeSet after class exercises
Supplement 14: cmake practice project notes (to be continued 4 / 22)
IDE Idea 自动编译 与 On Upate Action 、 On Frame Deactivation 的配置
[AI vision · quick review of today's sound acoustic papers, issue 3] wed, 20 APR 2022
Migrate from MySQL database to AWS dynamodb
383. 赎金信
[AI vision · quick review of robot papers today, issue 32] wed, 20 APR 2022
【论文阅读】【3d目标检测】Improving 3D Object Detection with Channel-wise Transformer
IEEE Transactions on Systems, Man, and Cybernetics: Systems(TSMC)投稿须知
win10, mysql-8.0.26-winx64.zip 安装
洛谷P1858 【多人背包】 (背包求前k优解)
mysql table 中增加列的SQL语句
【论文阅读】【3d目标检测】Voxel Transformer for 3D Object Detection
第四章 --- 了解标准设备文件、过滤器和管道
[pytoch foundation] torch Split() usage
Recursive call -- Enumeration of permutations
KVM error: Failed to connect socket to ‘/var/run/libvirt/libvirt-sock‘
win10, mysql-8.0.26-winx64. Zip installation
Key points of AWS eks deployment and differences between console and eksctl creation
The perfect combination of collaborative process and multi process
