当前位置:网站首页>MySQL小練習(僅適合初學者,非初學者勿進)
MySQL小練習(僅適合初學者,非初學者勿進)
2022-04-23 08:53:00 【一個熱愛編程的小白白】
哈哈,這是我們老師布置的作業,我在想用不用發博客呢?
最後想了一下,還是發出來吧,雖然很簡單,但是可以給那些剛學數據庫的朋友來練習一下。因為沒有答案,我也不知道對不對,如果有大佬發現錯誤,請指出來。
題目:
- 查詢各比特學生的學號、班級和姓名
- 查詢課程的全部信息
- 查詢數據庫中有哪些專業班級
- 查詢學時大於60的課程信息
- 查詢出生在1986年出生的學生的學號、姓名和出生日期
- 查詢三次作業成績都在80分以上的學號、課程號
- 查詢姓張的學生的學號、姓名和專業班級
- 查詢05級的男生信息
查詢沒有作業成績的學號和課程號
- 查詢學號為0538的學生的作業1總分
- 查詢選修了K001課程的學生人數
- 查詢數據庫中共有多少個班級
- 查詢選修三門以上(含3門)課程的學生的學號和作業1平均分,作業2平均分,作業3 平均分
如果小夥伴們是0基礎 也沒關系,看看下面這篇博客 然後再來做題。
1.創建數據庫
create database if not exists db2 ;
好了創建成功 ,然後我們打開控制臺
2.創建錶
1.創建學生錶
分析:
學號:字符型
姓名:字符型
性別:字符型 性別是一個字 所以 varchar(1)
專業班級:字符型
出生日期:時間類型 date
聯系電話:字符型 電話號碼11比特 varchar(11)即可。
drop table if exists student;
create table student
(
id varchar(10) comment '學號',
name varchar(10) NOT NULL comment '姓名',
gender char(1) comment '性別',
class varchar(20) comment '專業班級',
date date comment '出生日期',
iphone varchar(11) comment '聯系電話'
)
comment '學生錶';
select * from student;
2.創建課程錶
drop table if exists student_course;
create table student_course
(
course_id varchar(10) comment '課程號',
course_name varchar(15) comment '課程名',
course_number double unsigned comment '學分數',
student_time int unsigned comment '學時數',
teacher varchar(10) comment '任課教師'
)
comment '課程錶';
select *
from student_course;
3.學生作業錶
drop table if exists student_homework;
create table student_homework
(
course_id varchar(10) comment '課程號',
id varchar(10) comment '學號',
homework_1 int comment '作業1成績',
homework_2 int comment '作業2成績',
homework_3 int comment '作業3成績'
)
comment '學生作業錶';
select *
from student_homework;
3.添加數據
照著圖片上的數據一個個打出來的,嗚嗚嗚
1.學生錶
insert into student
values ('0433', '張豔', '女', '生物04', '1986-9-13', null),
('0496', '李越', '男', '電子04', '1984-2-23', '1381290xxxx'),
('0529', '趙欣', '男', '會計05', '1984-1-27', '1350222xxxx'),
('0531', '張志國', '男', '生物05', '1986-9-10', '1331256xxxx'),
('0538', '於蘭蘭', '女', '生物05', '1984-2-20', '1331200xxxx'),
('0591', '王麗麗', '女', '電子05', '1984-3-20', '1332080xxxx'),
('0592', '王海强', '男', '電子05', '1986-11-1', null);
查詢一下:
select * from student;
2.課程錶
INSERT INTO student_course
values ('K001', '計算機圖形學', 2.5, 40, '胡晶晶'),
('K002', '計算機應用基礎', 3, 48, '任泉'),
('K006', '數據結構', 4, 64, '馬躍先'),
('M001', '政治經濟學', 4, 64, '孔繁新'),
('S001', '高等數學', 3, 48, '趙曉塵');
查詢一下:
select *
from student_course;
3.學生作業錶
insert into student_homework values
('K001','0433',60,75,75),
('K001','0529',70,70,60),
('K001','0531',70,80,80),
('K001','0591',80,90,90),
('K002','0496',80,80,90),
('K002','0529',70,70,85),
('K002','0531',80,80,80),
('K002','0538',65,75,85),
('K002','0592',75,85,85),
('K006','0531',80,80,90),
('K006','0591',80,80,80),
('M001','0496',70,70,80),
('M001','0591',65,75,75),
('S001','0531',80,80,80),
('S001','0538',60,null,80);
查詢一下:
select *
from student_homework;
4.開始做題
1.查詢各比特學生的學號、班級和姓名
select id,class,name from student;
2.查詢課程的全部信息
select *
from student_course;
3.查詢數據庫中有哪些專業班級
select class from student;
4.查詢學時大於60的課程信息
select course_id,course_name from student_course where student_time>60;
5.查詢出生在1986年出生的學生的學號、姓名和出生日期
select id,name,date from student where date>=('1986-1-1') AND date<('1987-1-1');
6.查詢三次作業成績都在80分以上的學號、課程號
一開始我是用這個查詢了一遍:
select * from student_homework where homework_1>80 and homework_2>80 and homework_3>80;
發現啥也沒有,於是我看了一下數據 發現滿足三次作業成績都在80分以上的 沒有這種數據
故此題目:“查詢三次作業成績都在80分以上的學號、課程號” 應該包括80分
於是,我修改一下:
select * from student_homework where homework_1>=80 and homework_2>=80 and homework_3>=80;
7.查詢姓張的學生的學號、姓名和專業班級
錯誤示範:
由於幾百年沒寫SQL了, 我竟然寫成(給自己一巴掌):
select id,name,class from student where name = '張%';
select id,name,class from student where name like '張%';
8.查詢05級的男生信息
select * from student where class like '%05' and gender='男';
9.查詢沒有作業成績的學號和課程號
select id,course_id from student_homework where homework_1 is null or homework_2 is null or homework_3 is null ;
10.查詢學號為0538的學生的作業1總分
select sum(homework_1) '總分' from student_homework where id='0538';
11.查詢選修了K001課程的學生人數
select count(*) from student_homework where course_id='K001';
12.查詢數據庫中共有多少個班級
select count(*) from student where class is not null ;
13.查詢選修三門以上(含3門)課程的學生的學號和作業1平均分,作業2平均分,作業3 平均分
select student.id, avg(homework_1), avg(homework_2), avg(homework_3)
from student
left join student_homework on student.id = student_homework.id
group by student.id
having count(course_id) >= 3;
版权声明
本文为[一個熱愛編程的小白白]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230850315895.html
边栏推荐
- 怎样读取Excel表格到数据库
- 经典题目刷一刷
- Learn SQL injection in sqli liabs (Level 11 ~ level 20)
- DJ音乐管理软件Pioneer DJ rekordbox
- Single chip microcomputer nixie tube stopwatch
- L2-023 图着色问题 (25 分)(图的遍历)
- Non duplicate data values of two MySQL query tables
- Judgment on heap (25 points) two insertion methods
- Star Trek's strong attack opens the dream linkage between metacosmic virtual reality
- OneFlow学习笔记:从Functor到OpExprInterpreter
猜你喜欢
Please arrange star trek in advance to break through the new playing method of chain tour, and the market heat continues to rise
洋桃电子STM32物联网入门30步笔记一、HAL库和标准库的区别
資源打包關系依賴樹
四张图弄懂matplotlib的一些基本用法
Summary of solid problems
洋桃电子STM32物联网入门30步笔记二、CubeIDE下载、安装、汉化、设置
MySQL小练习(仅适合初学者,非初学者勿进)
xctf刷题小记
DJ音乐管理软件Pioneer DJ rekordbox
资源打包关系依赖树
随机推荐
After a circle, I sorted out this set of interview questions..
Technological innovation in government affairs in the construction of Digital Government
Notes on 30 steps of introduction to Internet of things of yangtao electronics STM32 III. Explanation of new cubeide project and setting
1099 establish binary search tree (30 points)
Brief steps to build a website / application using flash and H5
请问中衍期货安全靠谱吗?
Find the sum of simple types of matrices
Star Trek's strong attack opens the dream linkage between metacosmic virtual reality
Arbre de dépendance de l'emballage des ressources
Complete binary search tree (30 points)
Single chip microcomputer nixie tube stopwatch
是否同一棵二叉搜索树 (25 分)
洋桃电子STM32物联网入门30步笔记一、HAL库和标准库的区别
L2-022 rearrange linked list (25 points) (map + structure simulation)
Valgrind et kcachegrind utilisent l'analyse d'exécution
ONEFLOW learning notes: from functor to opexprinter
Introduction to matlab
GUI编程简介 swing
Flink reads MySQL and PgSQL at the same time, and the program will get stuck without logs
Go language self-study series | golang structure pointer