方便Mysql 巩固提升

创建表并插入数据:

-- ----------------------------

-- Table structure for student

-- ----------------------------

DROP TABLE IF EXISTS `student`;

CREATE TABLE `student` (

`id` int(11) NOT NULL AUTO_INCREMENT,

`sname` varchar(32) DEFAULT NULL,

`sage` int(11) DEFAULT NULL,

`ssex` varchar(8) DEFAULT NULL,

PRIMARY KEY (`id`)

) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=utf8;

-- ----------------------------

-- Records of student

-- ----------------------------

INSERT INTO `student` VALUES ('1', '刘一', '18', '男');

INSERT INTO `student` VALUES ('2', '钱二', '19', '女');

INSERT INTO `student` VALUES ('3', '张三', '17', '男');

INSERT INTO `student` VALUES ('4', '李四', '18', '女');

INSERT INTO `student` VALUES ('5', '王五', '17', '男');

INSERT INTO `student` VALUES ('6', '赵六', '19', '女');

-- ----------------------------

-- Table structure for teacher

-- ----------------------------

DROP TABLE IF EXISTS `teacher`;

CREATE TABLE `teacher` (

`id` int(11) DEFAULT NULL,

`tname` varchar(16) DEFAULT NULL

) ENGINE=InnoDB DEFAULT CHARSET=utf8;

-- ----------------------------

-- Records of teacher

-- ----------------------------

INSERT INTO `teacher` VALUES ('1', '叶平');

INSERT INTO `teacher` VALUES ('2', '贺高');

INSERT INTO `teacher` VALUES ('3', '杨艳');

INSERT INTO `teacher` VALUES ('4', '周磊');

-- ----------------------------

-- Table structure for course

-- ----------------------------

DROP TABLE IF EXISTS `course`;

CREATE TABLE `course` (

`id` int(11) DEFAULT NULL,

`cname` varchar(32) DEFAULT NULL,

`tid` int(11) DEFAULT NULL

) ENGINE=InnoDB DEFAULT CHARSET=utf8;

-- ----------------------------

-- Records of course

-- ----------------------------

INSERT INTO `course` VALUES ('1', '语文', '1');

INSERT INTO `course` VALUES ('2', '数学', '2');

INSERT INTO `course` VALUES ('3', '英语', '3');

INSERT INTO `course` VALUES ('4', '物理', '4');

-- ----------------------------

-- Table structure for sc

-- ----------------------------

DROP TABLE IF EXISTS `sc`;

CREATE TABLE `sc` (

`sid` int(11) DEFAULT NULL,

`cid` int(11) DEFAULT NULL,

`score` int(11) DEFAULT NULL

) ENGINE=InnoDB DEFAULT CHARSET=utf8;

-- ----------------------------

-- Records of sc

-- ----------------------------

INSERT INTO `sc` VALUES ('1', '1', '56');

INSERT INTO `sc` VALUES ('1', '2', '78');

INSERT INTO `sc` VALUES ('1', '3', '67');

INSERT INTO `sc` VALUES ('1', '4', '58');

INSERT INTO `sc` VALUES ('2', '1', '79');

INSERT INTO `sc` VALUES ('2', '2', '81');

INSERT INTO `sc` VALUES ('2', '3', '92');

INSERT INTO `sc` VALUES ('2', '4', '68');

INSERT INTO `sc` VALUES ('3', '1', '91');

INSERT INTO `sc` VALUES ('3', '2', '47');

INSERT INTO `sc` VALUES ('3', '3', '88');

INSERT INTO `sc` VALUES ('3', '4', '56');

INSERT INTO `sc` VALUES ('4', '2', '88');

INSERT INTO `sc` VALUES ('4', '3', '90');

INSERT INTO `sc` VALUES ('4', '4', '93');

INSERT INTO `sc` VALUES ('5', '1', '46');

INSERT INTO `sc` VALUES ('5', '3', '78');

INSERT INTO `sc` VALUES ('5', '4', '53');

INSERT INTO `sc` VALUES ('6', '1', '35');

INSERT INTO `sc` VALUES ('6', '2', '68');

INSERT INTO `sc` VALUES ('6', '4', '71');

1. 查询“001”课程比“002”课程成绩高的所有学生的学号

SELECT

a1.sid

FROM

(SELECT * FROM sc WHERE cid = 1) a1,

(SELECT * FROM sc WHERE cid = 2) a2

WHERE

a1.score > a2.score

AND a1.sid = a2.sid

select sid,GROUP_CONCAT(score),count(sid)from sc GROUP BY sid;

2. 查询平均成绩大于60分的学生的平均成绩

select sid,AVG(score) from sc GROUP BY sid HAVING AVG(score)>60;

3. 查询所有同学的学号、姓名、选课数、总成绩

select a1.id,a1.sname from student a1;

select sid,count(cid),sum(score) from sc GROUP BY sid;

SELECT

a1.id,

a1.sname,

a2.number,

a2.score

FROM

student a1,

(

SELECT

sid,

count(cid) AS number,

sum(score) AS score

FROM

sc

GROUP BY

sid

) a2

WHERE

a1.id = a2.sid;

4. 查询姓“周”的老师的个数

SELECT

count(DISTINCT(tname))

FROM

teacher

WHERE

tname LIKE '周%';

5.查询没学过“叶平”老师课的同学的学号、姓名

select id from course a where a.tid = (SELECT id from teacher where tname='叶平');

select sid,GROUP_CONCAT(cid ORDER BY cid ASC) as cids from sc GROUP BY sid;

SELECT

sid

FROM

(

SELECT

sid,

GROUP_CONCAT(cid ORDER BY cid ASC) AS cids

FROM

sc

GROUP BY

sid

) a

WHERE

! FIND_IN_SET('1', cids);

6. 查询学过“001”并且也学过编号“002”课程的同学的学号、姓名

方法一:

select sid from (select sid,GROUP_CONCAT(cid ORDER BY cid ASC) as cids from sc GROUP BY sid) a where FIND_IN_SET('1',cids) and FIND_IN_SET('2',cids);

SELECT id,sname from student where student.id in (select sid from (select sid,GROUP_CONCAT(cid ORDER BY cid ASC) as cids from sc GROUP BY sid) a where FIND_IN_SET('1',cids) and FIND_IN_SET('2',cids));

方法二:

select student.id,student.sname from student,sc where student.id = sc.sid and sc.cid = '001' and EXISTS(select * from sc sc2 where sc.sid = sc2.sid and sc2.cid='002');

7.查询学过“叶平”老师所教的所有课的同学的学号、姓名

SELECT student.id,student.sname from student where student.id in(SELECT DISTINCT(sid) from sc,course,teacher WHERE sc.cid = course.id and course.id = teacher.id and teacher.tname = '叶平');

8. 查询课程编号“002”的成绩比课程编号“001”课程低的所有同学的学号、姓名

select a2.sid from (select * from sc where sc.cid ='002') a1,(select * from sc where sc.cid ='001') a2 where a1.score < a2.score and a1.sid = a2.sid;

select student.id,student.sname from student where student.id in(select a2.sid from (select * from sc where sc.cid ='002') a1,(select * from sc where sc.cid ='001') a2 where a1.score < a2.score and a1.sid = a2.sid)

9. 查询所有课程成绩小于60分的同学的学号、姓名

select GROUP_CONCAT(score ORDER BY score asc) from sc GROUP BY sid

select * from student where student.id not in(select sid from student,sc where student.id = sc.sid and sc.score>60);

10.查询没有学全所有课的同学的学号、姓名

SELECT count(1) from course;

SELECT sid from sc GROUP BY sid HAVING count(cid)

select a.id,a.sname from student a,(SELECT sid from sc GROUP BY sid HAVING count(cid)

select a.id,a.sname from student a,sc b where a.id = b.sid GROUP BY a.id HAVING count(a.id)<4;

11.查询至少有一门课与学号为“1001”的同学所学相同的同学的学号和姓名

select DISTINCT(student.id),student.sname from student,sc where student.id = sc.sid and sc.cid in(SELECT sc.cid from sc where sc.sid='1');

12.把“SC”表中“叶平”老师教的课的成绩都更改为此课程的平均成绩

select course.id from course,teacher where course.tid = teacher.id and teacher.tname ='叶平';

select sc.cid,avg(score) from sc where sc.cid= 1 GROUP BY cid;

UPDATE sc set sc.score = 12 where sc.cid =5;

13.查询和“1002”号的同学学习的课程完全相同的其他同学学号和姓名

SELECT sc.sid,GROUP_CONCAT(cid ORDER BY cid ASC) as cids from sc GROUP BY sid;

SELECT cids from (SELECT sc.sid,GROUP_CONCAT(cid ORDER BY cid ASC) as cids from sc GROUP BY sid) a where a.sid = 1;

select * from (SELECT sc.sid,GROUP_CONCAT(cid ORDER BY cid ASC) as cids from sc GROUP BY sid) a where a.cids = (SELECT cids from (SELECT sc.sid,GROUP_CONCAT(cid ORDER BY cid ASC) as cids from sc GROUP BY sid) a where a.sid = 1);

14.删除学习“叶平”老师课的SC表记录

SELECT * from sc,course,teacher where sc.cid = course.id and course.id = teacher.id and teacher.tname = '叶平';

15.查询各科成绩最高和最低的分:以如下形式显示:课程ID,最高分,最低分

SELECT cid,MAX(score) as '最高分',MIN(score) as '最低分' from sc GROUP BY sc.cid

select cid,GROUP_CONCAT(score) from sc GROUP BY cid;

mysql课程表学时_Mysql 巩固提升 (学生表_课程表_成绩表_教师表)相关推荐

  1. 学生表 成绩表 课程表 教师表

    学生表: Student(s_id,s_name,s_birth,s_sex) –学生编号,学生姓名, 出生年月,学生性别 课程表: Course(c_id,c_name,t_id) – –课程编号, ...

  2. mysql学生表选课表课程表_mysql查询(学生表、课程表、选课表)

    ************************************************************ 为sc表中的sno和cno创建外键 alter table sc add fo ...

  3. mysql查询不同老师所教不同课程_mysql数据库面试题(学生表_课程表_成绩表_教师表)...

    Student(Sid,Sname,Sage,Ssex)学生表 Sid:学号 Sname:学生姓名 Sage:学生年龄 Ssex:学生性别 Course(Cid,Cname,Tid)课程表 Cid:课 ...

  4. Mysql数据库使用:学生选课系统,其中设计到三张表,分别为学生表,课程表,学生和课程对应的关联表。

    Mysql数据库使用 练习内容:主要针对学生选课系统而设计,其中设计到三张表,分别为学生表,课程表,学生和课程对应的关联表. 学生表主要用于存储学生信息,包括姓名,性别,地址,电话等信息 课程表主要用 ...

  5. mysql面试学生表_SQL笔试:Student学生表,Course 课程表,Sc选课表

    Student学生表(学号,姓名.性别.年龄.组织部门),Course 课程表(编号,课程名称),Sc选课表(学号,课程编号,成绩) 写一个SQL语句,查询选修了计算机原理的学生学号和姓名 selec ...

  6. mysql 创建学生表、课程表、学生选课表

    数据库系统概论(第五版)79页提供的三个表,为了我们上机操作容易,下面创建这三个表 学生-课程数据库中包含以下三个表 学生表:Student(Sno,Sname,Ssex,Sage,Sdept) 课程 ...

  7. MySQL(学生表、教师表、课程表、成绩表)多表查询

    多表查询SQL语句 1.表架构 student(sid,sname,sage,ssex) 学生表  course(cid,cname,tid) 课程表  sC(sid,cid,score) 成绩表  ...

  8. MySQL三表查询(学生表、课程表、成绩表)查询出语文成绩比数学成绩高的学生信息

    有三张表 学生表 课程表 成绩表 要求查处语文成绩低于数学成绩的学生信息 先去课程表中查出课程cid select cid from course where cname='语文'; select c ...

  9. [MySQL]学生表、教师表、课程表、授课表、成绩表的多表查询案例(1)

    多表查询环境:已有5个表,student为学生表,teacher为教师表,teaching为授课表,course为课程表,sc为成绩表. (1) 查询计算机工程系女学生的学生学号.姓名及考试成绩. S ...

最新文章

  1. java获取数组穷举_请教一下两个数组各取一个元素生成新的数组的穷举算法设计?...
  2. 宏转录组方法_高级转录组分析和R语言数据可视化第十二期 (线上线下同时开课)...
  3. 记录是一段旅程:记录Scribus可获得3课
  4. 【Es】Es 选主流程
  5. LINUX版本的 MYSQL大小写敏感的处理方式
  6. Python进阶-----类的内置方法__getattribute__
  7. 卫星导航开源代码汇总
  8. 按下()快捷键 可以迅速锁定计算机,电脑锁定的快捷键
  9. 2019 AI顶会时间表
  10. 初探ViewBinding
  11. win10 计算机网络密码怎么设置,win10系统提示windows安全 输入网络密码的设置教程...
  12. 华为OD机试 - 喊七(Python)
  13. 读研攻略(7)—从0到1,研究生第一篇SCI的诞生
  14. SIMCom芯片关于GPS定位信息的的解析(AT+CGNSINF)
  15. 分享两个解决Mac 访问Github的好方法
  16. 【整理】X86常用的汇编指令及寄存器
  17. Python4:操作列表
  18. 植入「电子神经」,瘫痪小鼠也能踢球,鲍哲南团队成果登Nature子刊
  19. 面经:涂鸦移动校招软件开发—Java
  20. 【嵌入式实验】南航嵌入式实验报告——定时器TIMx实验

热门文章

  1. 5亿整数的大文件,怎么排序 ?面试被问傻!
  2. Docker是传统的应用发布管理的终结者么?
  3. Linux里怎么进行路由跟踪,[Linux] traceroute 路由跟踪指令用例
  4. 北语18春《计算机网络技术》作业4,北语18春《计算机网络技术》作业4
  5. linux unzip命令不存在_15个常用基础命令Linux(很多人不知道!)
  6. tomcat jsvc java_opts_Tomcat 学习笔记(2) - 使用 jsvc 启动tomcat
  7. ajax清请求过程,JS深入基础之Ajax的请求过程
  8. java客户端程序用什么自动化测试_五大Java自动化测试框架
  9. VS Code vue 模板
  10. java 实现jpg、png、tif、gif 任意图像格式转换