mysql中的自连接查询
一、什么是自连接
自连接查询,顾名思义,就是自己和自己比较。
例如:分数表中,科目1有学生考试不及格,不及格的学生补考了,这个的话,同一个学生,同一个科目出现了2笔成绩,如果查询出成绩高的那一笔记录的话,需要分数表自己和自己比较。
二、数据准备
create database stuMS default charset=’utf8′;
use stuMS;
— 表1:学生信息表 Students
create table Students(
Scode int not null PRIMARY key auto_increment,
SName char(10) not null,
SAddress varchar(50) default ‘湖南长沙’,
SGrade int,
SEmail varchar(50) CHECK(SEmail like ‘%@%’),
SSex int CHECK(ssex=0 or ssex=1),
CreateTime datetime default NOW()
);
— 表2: 科目表 Course
create table Course(
CourseID int not null primary key,
CourseName varchar(50) not null
);
— — 表3:成绩表 Score
create table Score(
ScoreID int not null PRIMARY key,
CourseID int not null,
Studentid int not null,
Score smallint,
CONSTRAINT score_Studentid_fk foreign key(Studentid)
references Students(Scode),
CONSTRAINT score_Courseid_fk foreign key(CourseID)
references Course(CourseID)
);
— 4. 表1学生信息表插入数据
insert into Students(SName,saddress,ssex) VALUES(‘张三’,’北京’,1);
insert into Students(SName,saddress,ssex) VALUES(‘李四’,’上海’,0);
insert into Students(SName,saddress,ssex) VALUES(‘王五’,’广州’,1);
insert into Students(SName,saddress,ssex) VALUES(‘赵六’,’深圳’,0);
insert into Students(SName,saddress,ssex) VALUES(‘田七’,’长沙’,1);
— 5. 表2 学生课程表中输入数据
insert into Course VALUES(1,’语文’);
insert into Course VALUES(2,’数学’);
insert into Course VALUES(3,’英语’);
insert into Course VALUES(4,’测试’);
— 6. 表3 成绩表中插入课程1的成绩,要求有人不及格
INSERT into score VALUES(1,1,1,100);
INSERT into score VALUES(2,1,2,88);
INSERT into score VALUES(3,1,3,50);
INSERT into score VALUES(4,1,4,70);
INSERT into score VALUES(5,1,5,46);
— 7. 表3 成绩表中插入课程1的补考成绩,补考后成绩及格
INSERT into score VALUES(6,1,3,80);
INSERT into score VALUES(7,1,5,82);
三、分步练习
–练习目标: 使用自身查询,查询科目1 的成绩,如果重考,取分数高的那一笔
select * from score where courseid=1;
— 练习1:找出重考分数高的成绩
select a.*
from score a,score b
where a.studentid=b.studentid
and a.courseid=b.courseid
and a.score > b.score
and a.courseid=1;
— 练习2:找出科目1不及格的分数
select a.*
from score a,score b
where a.studentid=b.studentid
and a.courseid=b.courseid
and a.score < b.score
and a.courseid=1;
— 练习3:找出需要排除的数据的scoreid
select a.scoreid
from score a,score b
where a.courseid=b.courseid
and a.studentid=b.studentid
and a.score<b.score
and a.courseid=1;
–练习4:最后的结果
select *
from score
where scoreid not in — 排除的数据的不显示,其他数据显示
(select a.scoreid
from score a,score b
where a.courseid=b.courseid
and a.studentid=b.studentid
and a.score<b.score
and a.courseid=1
)and courseid=1;
如果有对软件测试感兴趣的小伙伴可以加群了解更多:点击进群
mysql中的自连接查询相关推荐
- MySQL中的分页查询
MySQL中的分页查询 一.MySQL分页查询原则 在MySQL数据库中使用limit子句进行分页查询: MySQL分页中开始位置为0: 分页子句在查询语句的最后侧: 二.Limit子句(较为常 ...
- MySQL中的各种查询
文章目录 MySQL中的各种查询 基础查询 条件查询 排序查询 常见函数查询 分组查询 连接查询 内连接 外连接 交叉连接 子查询 联合查询 MySQL中的各种查询 基础查询 条件查询 #语法:sel ...
- any在mysql语句中用法,MySQL中,子查询中可以使用运算符ANY,它表示的意思是______。...
MySQL中,子查询中可以使用运算符ANY,它表示的意思是______. 答:至少一个值满足条件 According to your textbook, the main purpose of a s ...
- 在mysql中通配符_mysql查询中通配符的使用
mysql查询中通配符的使用 在mysql查询中经常会使用通配符,并且mysql的通配符和pgsql的存在区别(稍候再讨论),而且mysql中还可以使用正则表达式. SQL模式匹配: "_& ...
- MySQL中的关联查询
MySQL中的关联查询 (1)Question:关联是什么 关联是SQL语言中使用SELECT操作表的一种操作机制,用来联系两个或者多个表.SELECT是SQL中的查询语句,用于查询数据库中的数据.将 ...
- MySQL中嵌套子查询删除出错解决方案
MySQL中嵌套子查询做删除操作会出错,例如下面的SQL: delete from table1 where number in (select number from table2) 执行提示: Y ...
- MySQL中,关联查询的3种写法…
原文地址:MySQL中,关联查询的3种写法(USING/ON) 作者:王小安 看看下面三个关联查询的 SQL 语句有何区别? [sql] view plain copy SELECT * FROM ...
- MySQL中的数据查询
文章目录 1 简单查询 1.1 查询所有字段数据 1.2 查询指定字段数据 1.3 DISTINCT查询 1.4 IN查询 1.5 BETWEEN AND查询 1.6 LIKE模糊查询 1.7 对查询 ...
- MySQL中向下查询_mysql
@ MySQL讲解 一.启动服务 用管理员身份运行命令提示符 停止服务 net stop + 服务名 启动服务 net start + 服务名 查询服务器连接所用端口信息 select @@port; ...
最新文章
- RealPlayer 15正式发布 简体中文版下载
- mysql ignore 1 lines_20190716MySQL基础操作(一)
- 盒马mini带客流,老菜场攒烟火气,新老菜场交融相映成辉
- ajax 导致 css 延迟_在H5,小程序,uni-app中使用animate.css
- 20191011每日一句
- 电脑端图纸设计辅助工具-AutoCAD提供下载
- 【积分变换】积分变换常用公式定理与方法
- JVM监控及诊断工具之JConsole
- 2022年中国汽车维修设备市场现状研究分析
- vFORUM 2018,开启多云未来
- 我查查 6.6 去校验分析
- 整理了MariaDB和MySQL数据库历年发布版本和对应关系,方便记忆命令。
- 如何在RK3588上面使用摄像头实时实现物体识别?
- 阿里云大数据助理工程师认证考试考什么内容?
- 如何让git commit更简洁
- 两个对象值相同(x.equals(y) == true),但却可有不同的hash code,这句话对不对
- 数学基础 - 第十五章 分式
- spring factory-method和factory-bean 使用
- Android 音频源码分析——AndroidRecord录音(一)
- java计算机毕业设计HTML5游戏网站设计与实现源码+mysql数据库+系统+lw文档+部署
热门文章
- TI蓝牙低功耗技术BLE课程
- Oxford Buildings Dataset 图像数据集 下载地址| 牛津建筑物 |
- 麻省理工博士退学开网店两年身家过千万
- 人生不会是场戏的^_^
- $.get()调用php_jquery get ($.get) 事件用法与分析
- 作为一名数据科学从业者,你应该知道的P值
- IDEA中使用git,并且配置git忽略文件
- 关于Yii1和Yii2的ActiveRecord活动记录对象关联查询时底层调用SQL语句的不同
- Word怎么撤销上一步操作
- 苹果8p电池多少毫安的_万元苹果12不给充电头,两千多毫安电池配5G,开始破发了...