一个对inner jion ...on 的sql多表联合查询的练习
create database practiceSql; use practiceSql;
--
create table student(
`id` bigint not null auto_increment comment '主键id自增',
`name` varchar(20) not null comment '学生名',
`student_num` int not null comment '学号',
`sex` varchar(4) not null comment '性别',
`age` int not null comment '年龄',
`college` varchar(50) not null comment'学院',
primary key(id),
key idx_name(name),
key idx_student_num(student_num),
key idx_college (college)
)engine=InnoDB Auto_increment=1000 default charset=utf8 comment='学生表'; --插入10个学生信息
insert into student(name,student_num,sex,age,college)
values ('张三','','男','','计算机学院'),
('李红','','女','','计算机学院'),
('黄山','','男','','美术学院'),
('李丽','','女','','美术学院'),
('何冲','','男','','计算机学院'),
('欧皇','','男','','英语学院'),
('马涛','','女','','英语学院'),
('廖彤','','女','','计算机学院'); --课程表
create table course (
`course_id` bigint not null auto_increment comment'课程id',
`student_num` int not null comment'学号',
`student_name` varchar(20) not null comment'姓名',
`name` varchar(50) not null comment '课程名字',
primary key(course_id),
key idx_student_num(student_num),
key idx_student_name(student_name)
) engine=innoDB auto_increment=100 default charset=utf8 comment='课程表'; --向课程表插入信息 insert into course(student_num,student_name,name)
values (1,'张三','数学'),
(2,'李红','英语'),
(1,'张三','英语'),
(1,'张三','数学'),
(2,'李红','语文'),
(1,'张三','数学'),
(3,'黄山','语文'); --教师表
create table teacher(
`teacher_id` bigint not null auto_increment comment'教师id',
`name` varchar (20) not null comment'教师名字',
`sex` varchar(4) not null comment '性别',
`age` int not null comment '年龄',
`course` varchar (50) not null comment '所教科目',
primary key(teacher_id),
key idx_name(name),
key idx_course(course)
) engine=innoDB auto_increment=200 default charset=utf8 comment='教师表'; --设置主键自增
alter table teacher change teacher_id teacher_id bigint not null auto_increment; insert into teacher(name,sex,age,course) values
('Mr.Lee','男',40,'数学'),
('Mr.Hu','女',33,'英语'),
('Mr.Chen','男',38,'语文'); --查询张三的所有信息 课程名字,课程的老师
--这个不行
select distinct id , student.name as sname,student.student_num as snum,student.sex as ssex,student.age as sage,college
,course.name as cname,teacher.name as tname from student,teacher,course where student.name='张三'; --这个也不行
select distinct id , student.name as sname,student.student_num as snum,student.sex as ssex,student.age as sage,college
,course.name as cname,teacher.name as tname from student,teacher,course where course.student_name='张三'; --这个可以
select distinct id , s.name as sname,s.student_num as snum,s.sex as ssex,s.age as sage,college
,c.name as cname,t.name as tname from ((student s inner join course c on c.student_name = s.name )
inner join teacher t on t.course=c.name)
where s.name ='张三';
--没有distinct
select id , s.name as sname,s.student_num as snum,s.sex as ssex,s.age as sage,college
,c.name as cname,t.name as tname from ((student s inner join course c on c.student_name = s.name )
inner join teacher t on t.course=c.name)
where s.name ='张三'; --查询张三的所有信息 课程名字, select distinct id , s.name as sname,s.student_num as snum,s.sex as ssex,s.age as sage,college
,c.name as cname from student s inner join course c on c.student_name = s.name where s.name ='张三';
--更新老师课程
update teacher set course = "数学" where course ='数学老师' ;
一个对inner jion ...on 的sql多表联合查询的练习的更多相关文章
- SQL多表联合查询
通过连接运算符可以实现多个表查询.连接是关系数据库模型的主要特点,也是它区别于其它类型数据库管理系统的一个标志. 在关系数 据库管理系统中,表建立时各数据之间的关系不必确定,常把一个实体的所有信息存放 ...
- Linq To Sql多表联合查询
var pro = (from ps in db.ProductInfoes join pt in db.ProductTypees on ps.productType equals pt.pType ...
- SQL多表联合查询(交叉连接,内连接,外连接)
连接查询: 交叉连接: 交叉连接返回的结果是被连接的两个表中所有数据行的笛卡尔积,也就是返回第一个表中符合查询条件的数据航数乘以第二个表中符合,查询条件的数据行数,例如department ...
- sql两表联合查询
SELECT yt_fault_componentId FROM yt_fault_component a join yt_fault_assembly b on a.yt_fault_assembl ...
- sql 多表联合查询更新
sqlserver: update A a set a.i = b.k from B b where a.key = b.key oracle : update A a set a.i = (sele ...
- SQL多表连接查询(详细实例)
转载博客:joeleo博客(http://www.xker.com/page/e2012/0708/117368.html) 本文主要列举两张和三张表来讲述多表连接查询. 新建两张表: 表1:stud ...
- SQL多表连接查询
SQL多表连接查询 本文主要列举两张和三张表来讲述多表连接查询. 新建两张表: 表1:student 截图如下: 表2:course 截图如下: (此时这样建表只是为了演示连接SQL语句,当然实际 ...
- 图解SQL多表关联查询
图解SQL多表关联查询 网上看了篇文章关于多表连接的,感觉很好,记录下来,以便日后自己学习 内连接 左连接 右连接 全外连接 1. 查两表关联列相等的数据 ...
- springdata 查询思路:基本的单表查询方法(id,sort) ---->较复杂的单表查询(注解方式,原生sql)--->实现继承类---->复杂的多表联合查询 onetomany
springdata 查询思路:基本的单表查询方法(id,sort) ---->较复杂的单表查询(注解方式,原生sql)--->实现继承类---->复杂的多表联合查询 onetoma ...
随机推荐
- python3入门之赋值语句介绍
获得更多资料欢迎进入我的网站或者 csdn或者博客园 本节主要介绍赋值语句,以及几种特殊的赋值.下面附有之前的文章: python3入门之print,import,input介绍 python入门之字 ...
- MPMoviePlayerViewController不能播放本地mp4的解决办法.
之前一直用MPMoviePlayerViewController进行网络播放,最近下载文件然后本地播放,发现怎么播放都是黑屏.后来发现MPMoviePlayerViewController的本地URL ...
- MB Star C3 vs MB Star C4
New Mercedes Benz Star C3 is a professional diagnostic tool specially for mercedes benz cars. Merced ...
- $bzoj1021-SHOI2008\ Debt$ 循环的债务 $dp$
题面描述 \(Alice\).\(Bob\)和\(Cynthia\)总是为他们之间混乱的债务而烦恼,终于有一天,他们决定坐下来一起解决这个问题.不过,鉴别钞票的真伪是一件很麻烦的事情,于是他们决定要在 ...
- 转帖 利用伪元素和css3实现鼠标移入下划线向两边展开效果
原帖地址 https://www.cnblogs.com/yangjunfei/p/6739683.html 感谢分享 一.思路: 将伪元素:before和:after定位到元素底部中间,设置宽度 ...
- mysql语句插入前判断数据是否重复
在mysql中插入数据有时需要判断数据插入是否重复 语句编写:insert into 表(相应字段) select 相应字段 from dual where not exists (select 相应 ...
- IO流(一)字节流
1:io流体系:对数据进行读写操作.所以IO不是读就是写咯. 2:io流的相关的类:java.io包. 有关IO的操作都会产生IOException异常 3:io:参照物是程序, i:input.进来 ...
- 使用git配合idea使用oschina代码仓库初级教程
http://git.oschina.net/ 这个是开源中国的代码仓库是免费的,可以建100和仓库,私有也是免费的 第一步,创建 oschina代码仓库,这里就忽略了.很简单.去注册一个简单几步就可 ...
- ASP.NET MVC 域名泛解析设置
最近有个需求要做一个动态二级域名的网站,我们可以通过这样的方式去访问我们的网站 http://用户名.blog.com.而这里的用户名是根据程序的需要动态生成的.这里就会涉及到DNS服务器,要做相应的 ...
- 【c++】重载操作符
目录 输入和输出操作符 算术操作符和关系操作符 下标操作符 自加.自减操作符 成员访问操作符 1 输入和输出操作符 1.1 输出操作符 1.1.1 示例 #include <iostream& ...