From 3fabebeec103c4ea5b24a9d5466e87cf7ece9af1 Mon Sep 17 00:00:00 2001 From: 1 1 <2161737470@qq.com> Date: Sun, 19 Mar 2023 20:14:53 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E7=AC=AC=E5=85=AB=E6=AC=A1=E4=BD=9C?= =?UTF-8?q?=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...47\273\204\345\207\272\351\242\230\347\255\224\346\241\210.md" | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 "04 \346\235\216\346\230\216\345\201\245/20230316\347\254\254\345\205\255\347\273\204\345\207\272\351\242\230\347\255\224\346\241\210.md" diff --git "a/04 \346\235\216\346\230\216\345\201\245/20230316\347\254\254\345\205\255\347\273\204\345\207\272\351\242\230\347\255\224\346\241\210.md" "b/04 \346\235\216\346\230\216\345\201\245/20230316\347\254\254\345\205\255\347\273\204\345\207\272\351\242\230\347\255\224\346\241\210.md" new file mode 100644 index 0000000..e69de29 -- Gitee From 44fea8306a14617f78f9b6279d84fc639685b17f Mon Sep 17 00:00:00 2001 From: 1 1 <2161737470@qq.com> Date: Sun, 19 Mar 2023 20:28:16 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E7=AC=AC=E4=B9=9D=E6=AC=A1=E4=BD=9C?= =?UTF-8?q?=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...72\351\242\230\347\255\224\346\241\210.md" | 63 +++++++ ...72\351\242\230\347\255\224\346\241\210.md" | 163 ++++++++++++++++++ 2 files changed, 226 insertions(+) create mode 100644 "04 \346\235\216\346\230\216\345\201\245/20230318\351\231\210\346\266\233\345\207\272\351\242\230\347\255\224\346\241\210.md" diff --git "a/04 \346\235\216\346\230\216\345\201\245/20230316\347\254\254\345\205\255\347\273\204\345\207\272\351\242\230\347\255\224\346\241\210.md" "b/04 \346\235\216\346\230\216\345\201\245/20230316\347\254\254\345\205\255\347\273\204\345\207\272\351\242\230\347\255\224\346\241\210.md" index e69de29..0533eca 100644 --- "a/04 \346\235\216\346\230\216\345\201\245/20230316\347\254\254\345\205\255\347\273\204\345\207\272\351\242\230\347\255\224\346\241\210.md" +++ "b/04 \346\235\216\346\230\216\345\201\245/20230316\347\254\254\345\205\255\347\273\204\345\207\272\351\242\230\347\255\224\346\241\210.md" @@ -0,0 +1,63 @@ +~~~ sql +create database stu charset utf8; +use stu; + + create table user_id( + user_id int auto_increment unique key, + user_name varchar(10), + user_tel varchar(20), + ordertime date + ); + + create table `order`( + order_id int auto_increment unique key, + uesr_id int, + type varchar(10), + food varchar(10), + number int, + price int + ); + + insert into user_id (user_name,user_tel,ordertime) values +("张三",'19865645691','2023-3-13'), +("李四",'12345675691','2023-3-13'), +("王五",'12895678901','2023-3-13'), +("老六",'12345678691','2023-3-14'), +("小七",'18945678031',null), +("老八",'19345678561','2023-3-16'), +("朱九",'15845679911',null); + +insert into `order`(uesr_id,type,food,number,price) values +(1,'快餐','肯德基全家桶',1,50), +(1,'快餐','可乐',4,20), +(6,'饮料','柠檬水',2,16), +(2,'米饭','鱼香肉丝盖饭',5,60), +(2,'饮料','珍珠奶茶',3,22), +(3,'粉面类','螺蛳粉',4,28), +(4,'粉面类','凉拌面',1,10); + +-- 1.在用户表(user)新增一个性别字段user_sex,数据类型为char。 + alter table user_id add user_sex char; +-- 2.由于顾客3退单,请把此订单删除。 + delete from `order` where uesr_id=3; +-- 3.老八的电话号码换新了,请将他的号码更新为8888888888。 + update user_id set user_tel='8888888888' where user_name="老八"; +-- 4.查询order表里的最高金额和总金额。 + select max(price) 最高金额,sum(price) 总金额 from `order`; +-- 5.查询每个顾客分别订购了几个类别。 + select uesr_id,type,count(uesr_id) from `order` group by uesr_id,type; +-- 6.查询饮料的总金额 + select sum(number*price) 总金额 from `order` where type='饮料' +-- 7.查询订单表(order)里的订单号,类别(type),食物(food). + select order_id 订单号,type 类别,food 食物 from `order`; +-- 8.查询没吃饭的人的所有信息 + select * from user_id where user_id not in + (select distinct uesr_id from `order`); +-- 9.查询姓老的人的全部信息及订单信息 + select * from user_id where user_name like '老%'; +-- 10.查询总金额大于20的订单里最大值的用户信息,使用降序排列。 + select * from user_id where user_id= + (select uesr_id from + (select uesr_id,sum(number*price) s from `order` group by order_id having s>20 ) a order by a.s desc limit 1); +~~~ + diff --git "a/04 \346\235\216\346\230\216\345\201\245/20230318\351\231\210\346\266\233\345\207\272\351\242\230\347\255\224\346\241\210.md" "b/04 \346\235\216\346\230\216\345\201\245/20230318\351\231\210\346\266\233\345\207\272\351\242\230\347\255\224\346\241\210.md" new file mode 100644 index 0000000..84842f2 --- /dev/null +++ "b/04 \346\235\216\346\230\216\345\201\245/20230318\351\231\210\346\266\233\345\207\272\351\242\230\347\255\224\346\241\210.md" @@ -0,0 +1,163 @@ +~~~ sql +create database mtyk charset utf8; +use mtyk; + +create table j_teacher( + Tno int auto_increment primary key comment "教师编号", + Tname varchar(10) comment "教师姓名" + +); + +create table j_student( + Sno int auto_increment primary key comment "学生编号", + Sname varchar(10) unique key comment "学生姓名", + Sage date comment "学生年龄", + Ssex enum('男','女') comment "性别" +); + +create table j_course( + Cno int auto_increment primary key comment "课程编号", + Cname varchar(10) unique key comment "课程名称", + Tno int, + foreign key (Tno) references j_teacher(Tno) +); + +create table j_score( + Sno int, + Cno int, + Score int, + foreign key(Sno) references j_student(Sno), + foreign key(Cno) references j_course(Cno) +); + +insert into j_score values +(1,1,80), +(1,2,86), +(1,3,83), +(1,4,89), +(2,1,50), +(2,2,36), +(2,3,43), +(2,4,59), +(3,1,50), +(3,2,96), +(3,3,73), +(3,4,69), +(4,1,90), +(4,2,36), +(4,3,88), +(4,4,99), +(5,1,90), +(5,2,96), +(5,3,98), +(5,4,99), +(6,1,70), +(6,2,66), +(6,3,58), +(6,4,79), +(7,1,80), +(7,2,76), +(7,3,68), +(7,4,59), +(7,5,89); + +insert into j_student(sname,sage,ssex) VALUES +("张三","1980-1-23",'男'), +("李四","1982-12-12",'男'), +("张飒","1981-9-9",'男'), +("莉莉","1983-3-23",'女'), +("王弼","1982-6-21",'男'), +("王丽","1984-10-10",'女'), +("刘香","1980-12-22",'女'); + +insert into j_course(cname,tno) values +("企业管理",3), +("马克思",1), +("美团盛开",2), +("数据库",5), +("物理",8); + +insert into j_teacher(tname) values +("张老师"), +("王老师"), +("李老师"), +("赵老师"), +("刘老师"), +("向老师"), +("李文静"), +("叶平"); +-- 1、 修改刘香的美团盈开课程成绩为100分 + update j_score set score=100 where sno in (select sno from j_student where Sname="刘香") and cno in (select cno from j_course where cname="美团盛开"); + +-- 2、 将学生表中的sage段放在ssex段后面 + alter table j_student modify sage datetime after ssex; +-- 3、 教师表添加一个用于统计状态的列 名字为now 默认值为有课 + alter table j_teacher add now varchar(5) default "有课"; +-- 4、 将没有上课的老师的now列值设置为无课 + update j_teacher set now="无课" where tno not in(select Tno from j_course ); +-- 5、 修改课程表中的企业管理为比遛旺 + update j_course set Cname="比遛旺" where Cname="企业管理"; +-- 6、 查询课程1的成绩比课程2的成绩 高 的所有学生的学号。 + select a.Sno from j_score a,j_score b where a.Sno=b.Sno and a.Cno='1' and b.Cno='2' and a.score>b.score; +-- 7、 查询平均成绩大于60分的同学的学号和平均成绩。 + select sno, avg(score) s from j_score group by sno having s>60; +-- 8、 查询所有同学的学号、姓名、选课数、总成绩。 + select st.sno 学号,sname 姓名,count(sc.Cno) 选课数,sum(score) 总成绩 from j_student st left join j_score sc on st.Sno=sc.Sno group by sc.sno ; +-- 9、 查询姓“李”的学生的个数。 + select count(*) from j_student where sname like "李%"; +-- 10、 查询没学过“叶平”老师课的同学的学号、姓名。 + select sno,sname from j_student where sno != + (select sno from j_score where cno =(select cno from j_course where tno=(select tno from j_teacher where tname="叶平"))); +-- 11、 查询同时学过课程1和课程2的同学的学号、姓名。 + select sno,sname from j_student where sno in + (select sno from j_score where cno in (1,2) group by sno); +-- 12、 查询学过“叶平”老师所教所有课程的所有同学的学号、姓名。 + select sno,sname from j_student where sno= + (select sno from j_score where cno= + (select cno from j_course where tno= + (select tno from j_teacher where tname="叶平"))); +-- 13、 查询 课程编号1的成绩 比 课程编号2的成绩 高的所有同学的学号、姓名。 + select sno,sname from j_student where sno in + (select a.sno from j_score a ,j_score b where a.sno=b.sno and a.Cno='1' and b.Cno='2' and a.Score>b.Score); +-- 14、 查询所有课程成绩小于60分的同学的学号、姓名。 + select sno,sname from j_student where sno=(select sno from j_score group by sno having max(score)<60); +-- 15、 查询所有课程成绩大于60分的同学的学号、姓名。 + select sno,sname from j_student where sno in(select sno from j_score group by sno having min(score)>60); +-- 16、 查询没有学全所有课的同学的学号、姓名 + select sno,sname from j_student where sno != + (select sno from j_score group by sno having count(cno)= + (select count(cno) from j_course)); +-- 17、 查询至少有一门课程 与 学号为1的同学所学课程 相同的同学的学号和姓名 + select st.sno ,sname from j_student st,j_score sc where st.sno=sc.sno and st.sno<>1 and sc.cno in (select cno from j_score where sno =1)group by st.sno; +-- 18、 查询和2号同学学习的课程完全相同的其他同学学号和姓名。 + select sno from J_SCORE where sno not in (select sno from J_SCORE where cno not in (select cno from J_SCORE where sno=2)) group by sno having count(sno)=(select count(cno) from J_SCORE where sno=2); +-- 19、 查询各科成绩最高分和最低分。 + select cno,max(score),min(score) from j_score group by cno; +-- 20、 查询每门课程被选修的学生数。 + select cname,a.s from j_course c right join + (select cno,count(sno) s from j_score group by cno) a on c.cno=a.cno +-- 21、 查询出只选修了一门课程的全部学生的学号和姓名。 + select sno,sname from j_student where sno = + (select sno from j_score group by sno having count(cno)=1); +-- 22、 查询同名同性学生名单,并统计同名人数。 + select sname,ssex,count(sname) from (select a.sname,a.ssex from J_STUDENT a , J_STUDENT b where a.sname=b.sname and a.ssex=b.ssex and a.sno!=b.sno) c group by sname,ssex; +-- 23、 查询选修“叶平”老师所授课程的学生中,成绩最高的学生姓名及其成绩。 + select sname,Score from J_SCORE,J_STUDENT where J_SCORE.sno=J_STUDENT.sno and cno=(select cno from J_COURSE where tno=(select tno from J_TEACHER where tname='叶平')) order by score desc limit 1; +-- 24、 查询不同课程成绩相同的学生的学号、课程号、学生成绩。 + select a.sno,a.Cno,a.Score from j_score a ,j_score b where a.Score=b.Score and a.Cno<>b.Cno; +-- 25、 查询每门课程成绩最好的前两名的学生ID + select a.sno,a.cno,a.score from j_score a where (select count(*) from j_score b where a.cno=b.cno and a.score<=b.score)<=2; +-- 26、 检索至少选修了5门课程的学生学号。 + select sno from j_score GROUP BY sno having count(*)>=5; +-- 27、 查询没学过“叶平”老师讲授的任一门课程的学生姓名。 + select sno,sname from J_STUDENT where sname not in(select J_STUDENT.sname from J_STUDENT left join J_SCORE on J_STUDENT.sno = J_SCORE.sno left join J_COURSE on J_COURSE.cno = J_SCORE.cno left join J_TEACHER on J_TEACHER.tno=J_COURSE.tno where tname = '叶平'); +-- 28、 查询两门以上不及格课程的同学的学号及其平均成绩。 + select sno,avg(score) '平均成绩' from j_score where score<60 group by sno having count(*)>2; +-- 29、 查询最受欢迎的老师(选修学生最多的老师)。 + select tname from J_SCORE a,J_COURSE,J_TEACHER where a.cno=J_COURSE.cno and J_TEACHER.tno=J_COURSE.tno group by a.cno having count(a.cno)=(select max(c.b)from (select count(cno) b from J_SCORE group by cno) c); +-- 30、 修改课程表名为给乱给要酒要 + alter table J_COURSE rename 给乱给要酒要; + + +~~~ + -- Gitee