/*
Navicat MySQL Data Transfer
Source Server : 三丰153
Source Server Type : MySQL
Source Server Version : 80018
Source Host : 43.226.148.253:3306
Source Schema : seata-test
Target Server Type : MySQL
Target Server Version : 80018
File Encoding : 65001
Date: 28/03/2021 10:18:44
*/SET NAMES utf8mb4;SET FOREIGN_KEY_CHECKS =0;-- ------------------------------ Table structure for student-- ----------------------------DROPTABLEIFEXISTS`student`;CREATETABLE`student`(`id`bigint(20)NOTNULLAUTO_INCREMENTCOMMENT'自动编号',`sno`bigint(20)NOTNULLCOMMENT'学号',`name`varchar(15)CHARACTERSET utf8mb4 COLLATE utf8mb4_unicode_ci NOTNULLCOMMENT'学生姓名',`sex`int(2)NULLDEFAULTNULLCOMMENT'1-男,2-女',`cno`bigint(20)NULLDEFAULTNULLCOMMENT'课程号',`score`float(64,0)NULLDEFAULTNULLCOMMENT'分数',PRIMARYKEY(`id`)USINGBTREE)ENGINE=InnoDBAUTO_INCREMENT=63CHARACTERSET= utf8mb4 COLLATE= utf8mb4_unicode_ci ROW_FORMAT = Compact;-- ------------------------------ Records of student-- ----------------------------INSERTINTO`student`VALUES(36,4,'zhangsan4',1,2,99);INSERTINTO`student`VALUES(38,5,'zhangsan5',1,6,47);INSERTINTO`student`VALUES(40,6,'zhangsan6',1,7,37);INSERTINTO`student`VALUES(42,5,'zhangsan5',1,7,98);INSERTINTO`student`VALUES(43,6,'zhangsan6',1,1,63);INSERTINTO`student`VALUES(44,1,'zhangsan1',1,6,81);INSERTINTO`student`VALUES(45,5,'zhangsan5',1,1,80);INSERTINTO`student`VALUES(46,3,'zhangsan3',1,2,99);INSERTINTO`student`VALUES(47,4,'zhangsan4',1,1,73);INSERTINTO`student`VALUES(48,4,'zhangsan4',1,7,53);INSERTINTO`student`VALUES(49,2,'zhangsan2',1,1,47);INSERTINTO`student`VALUES(50,4,'zhangsan4',1,6,85);INSERTINTO`student`VALUES(51,3,'zhangsan3',1,1,64);INSERTINTO`student`VALUES(52,5,'zhangsan5',1,2,53);INSERTINTO`student`VALUES(53,6,'zhangsan6',1,2,44);INSERTINTO`student`VALUES(54,6,'zhangsan6',1,6,77);INSERTINTO`student`VALUES(55,2,'zhangsan2',1,7,58);INSERTINTO`student`VALUES(56,3,'zhangsan3',1,7,97);INSERTINTO`student`VALUES(57,2,'zhangsan2',1,6,68);INSERTINTO`student`VALUES(58,2,'zhangsan2',1,2,59);INSERTINTO`student`VALUES(59,1,'zhangsan1',1,1,88);INSERTINTO`student`VALUES(60,1,'zhangsan1',1,2,84);INSERTINTO`student`VALUES(61,3,'zhangsan3',1,6,66);INSERTINTO`student`VALUES(62,1,'zhangsan1',1,7,89);SET FOREIGN_KEY_CHECKS =1;
deletefrom
student
where id notin(selectmin(temp.id)as id from(select*from student)astempgroupbytemp.cno,temp.sno,temp.name,temp.score
)
二、课程题
2.1 查询每门课程成绩都大于80分学生的学号
表 student
name
score
course
A
85
语文
A
75
数学
A
82
英语
B
75
语文
B
89
数学
B
79
英语
天使美眉
90
语文
天使美眉
100
数学
天使美眉
100
英语
#SQL1:select name from student
groupby name havingmin(score)>80;#SQL2:select name from student
groupby name havingcount(score)=sum(casewhen score >0then1else0end)#SQL3select name from student
where name notin(selectdistinct(name)from student where socre <=80);
2.2 查询课程001的成绩大于课程002成绩的学号
student表:sno(学号),sname(姓名),sex(性别),dept(系)
course课程表:cno(课程号),课程名(cname)
sc选课表:sno,cno,grade(成绩)
#SQL1select sno from(select sc.*from sc,course where sc.cno = course.cno and cname ='001') one
innerjoin(select sc.*from sc,course where sc.cno = course.cno and cname ='002') two
on one.sno = two.sno where one.grade > two.grade;
三、推测题
3.1 已知表中推测sql
time
grade
2005-05-09
胜
2005-05-09
胜
2005-05-09
负
2005-05-09
负
2005-05-10
胜
2005-05-10
负
2005-05-10
负
如果要生成下列结果, 该如何写sql语句
time
胜
负
2005-05-09
2
2
2005-05-10
1
2
#SQL1selecttime,sum(case grade ='胜'when1else0end) 胜,sum(case grade ='负'when1else0end) 负 from tab
groupbytime;#SQL2select t1.time,t1.sucess as 胜,t2.fail as 负
(selecttime,count(1) sucess from tab where grade ='胜'groupbytime) t1,(selecttime,count(1) fail from tab where grade ='负'groupbytime) t2
where t1.timeand t2.time;#SQL3select t1.time,t1.sucess as 胜,t2.fail as 负
(selecttime,count(1) sucess from tab where grade ='胜'groupbytime) t1 innerjoin(selecttime,count(1) fail from tab where grade ='负'groupbytime) t2
on t1.timeand t2.time;
3.2 表中有A B C三列,用SQL语句实现:当A列大于B列时选择A列否则选择B列,当B列大于C列时选择B列否则选择C列
selectcase(when A > B then A else B end),case(when B > C then B else C end)from tab ;
select dep
(select yj from tab1 where mon ='一月份'and tab1.dep = t.dep ) 一月份,(select yj from tab1 where mon ='二月份'and tab1.dep = t.dep) 二月份,(select yj from tab1 where mon ='三月份'and tab1.dep = t.dep) 三月份
from tab2 t ;