一个对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 ...
随机推荐
- Bi-shoe and Phi-shoe(欧拉筛)
Bamboo Pole-vault is a massively popular sport in Xzhiland. And Master Phi-shoe is a very popular co ...
- java数据结构和算法学习笔记
第一章 什么是数据结构和算法 数据结构的概述 数据结构是指 数据再计算机内存空间或磁盘空间中的组织形式 1.数据结构的特性 数据结构 优点 缺点 数组 插入快,如果知道下标可以快速存取 查找和删除慢 ...
- 数组和arguments
Arguments(Array-Like Objects) arguments对象是所有(非箭头)函数中都可用的局部变量 拥有四个属性(按照规范来说只有三个了----caller) arguments ...
- QueryRunner(DBUtils) 结果集实例
转自:http://www.cnblogs.com/myit/p/4272824.html# 单行数据处理:ScalarHandler ArrayHandler MapHandler ...
- SpringCloud---消息驱动的微服务---Spring Cloud Stream
1.概述 1.1 Spring Cloud Stream:用来 为微服务应用 构建 消息驱动能力的框架: 可基于SpringBoot来创建独立.可用于生产的Spring应用程序: 使用Sp ...
- java实现HTTP请求的三种方式
目前JAVA实现HTTP请求的方法用的最多的有两种:一种是通过HTTPClient这种第三方的开源框架去实现.HTTPClient对HTTP的封装性比较不错,通过它基本上能够满足我们大部分的需求,Ht ...
- oracle dump的使用心得
使用DS开发的时候,有的时候会遇到一个问题:数据库层面定义的空格与DS自已定义的空格概念不一致,导致生成的数据会有一定的问题. 举例来说: 在数据库里面定义CHAR(20),如果插入的字符不足20的时 ...
- ruby中的== eql?和equal?区别
原文 http://www.wellho.net/mouth/985_Equality-in-Ruby-eql-and-equal-.html Equality in Ruby - == eql? a ...
- CentOS6.4 安装Maven及Nexus仓库代理
本文安装的apache-maven-3.5.0-bin.tar.gz,nexus-2.9.0-04-bundle.tar.gz 1.由于网络并不是特别好我这边是通过本地下载过来,通过sftp上传至Ce ...
- WEB前端笔记
HTML+CSS部分 IE6使用PNG的透明问题 解决关键字 DD_belatedPNG_0.0.8a.js Google或百度一下,下载之 <!--[if lte IE 6]> < ...