一个对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 ...
随机推荐
- P4173 残缺的字符串
题目链接 题意分析 啥 ? ? ? \(FFT\)做字符串匹配 可是就是这样 我们定义匹配函数 我们定义\(A\)是匹配串 \(B\)是被匹配串 我们当前到达\(B\)串的\(x\)位置 \[P(x) ...
- 在Mondrian Virtual OLAP Cube中观察星座模型多事实表度量值的聚合
这样设置的Schema文件会怎么样呢?用Saiku预览一下. 如果这时候想同时引用两个项目进行计算就会出问题了.那么这种情况怎么解决? 参考网上一段实现思路 <VirtualCube name= ...
- 关于云主机Thinkphp框架Session跨页失效的问题
在网站部署到云主机之后,前台一直能够正常显示,后台确登录不上去,验证码也无法显示,研究半天,才确定是Session跨页传递失效的问题.找网上各种解决方法,都是关于Php.ini文件的设置,可又解决不了 ...
- [Shell]如何获取Maven工程的project.version信息
问题: 今天遇到Shell中如何能获取Maven项目工程中的project.version信息的问题 解决方案: 使用Maven的Exec 插件 #! /bin/bash MVN_VERSION=$( ...
- 线性代数与simplex
线性方程组: \(i:1-n\) \(j:1-m\) \({\begin{cases}a_{11}x_1+a_{12}x_2+a_{13}x_3+\cdots+a_{1n}x_n=b_1\\a_{21 ...
- 对java的理解
一门编程语言,分三部分. 核心语法,库,数据结构.
- Laravel 控制器 Controller 传值到 视图 View 的几种方法总结
单个值的传递 with public function index() { $test = "测试"; return view('test.index')->with(' ...
- python+selenium打开浏览器报错问题
报关键字,升级selenium版本 若打开IE浏览器,停在IE界面,无法跳转对应的地址,设置一下IE的页面缩放,设置为100%
- Android deeplink和AppLink原理
APP开发中经常会有这种需求:在浏览器或者短信中唤起APP,如果安装了就唤起,否则引导下载.对于Android而言,这里主要牵扯的技术就是deeplink,也可以简单看成scheme,Android一 ...
- 在vue中import()语法不能传入变量
解决办法: 一定要用变量的时候,可以通过字符串模板来提供部分信息给webpack:例如import(`./path/${myFile}`), 这样编译时会编译所有./path下的模块,但运行时确定my ...