一个对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 ...
随机推荐
- CH5101 LCIS
CH5101 LCIS 题意: 求两个长度不超过3000的序列的最长公共上升子序列 思路: 朴素解法:用f[i,j]表示a1~ai与b1~bj可以构成的以bj为结尾的LCIS的长度,三重循环求解: ; ...
- SQLAlachemy 自动提交配置 SQLALCHEMY_COMMIT_ON_TEARDOWN
挖坑:自动提交省去了每次 commit,添加数据对象后立马取 id 返回None 填坑 :立马要取 id 的地方 commit一下
- JavaScript DOM编程艺术 笔记(二)语句操作
操作 var total = (1+4)*5; year = year +1; year++; var message = "i am" + "girl"; 是 ...
- 4G和有线网络的自动切换
最近项目有个需求,把移动服务器设备(Ubuntu14.04)安装4G模块,但如果连接有线时,可以自动切换到有线,以降低移动流量带来的费用. 以下是我实现的方法(经过一番痛苦的摸索) 1. 脚本/opt ...
- 导出excel设置样式(Aspose.Cells)
Aspose.Cells.Style style = xlBook.Styles[xlBook.Styles.Add()];style1.Pattern = Aspose.Cells.Backgrou ...
- java转pdf(html转为pdf),解决中文乱码,标签不规范等问题
第一步,下载jar包以及建对应的文件夹.注意pd4ml的jar要选择pro版本.然后建一个pd4fonts.properties 里面对应的字体. SimSun = simsun.ttf 前面为变量名 ...
- ionic3打包打包安卓apk详细过程以及遇到的问题
1.jdk和sdk的安装以及环境变量配置参考打包详解 上述连接已经从下载安装jdk以及sdk的环境变量配置到打包的流程以及很详细了.但是在我自己安装打包的过程中遇到了这篇文章中没有遇到的问题,下面图文 ...
- Unity 动画 命名
unity标准的动画命名格式是 ?模型名@动画名 如boss1@idle ,这样导入进来之后unity会自动给这个动画命名为idle而不是Take 001.
- 2016424王启元 Exp2 后门原理与实践
一.实验准备 1.在实验前关闭或退出了防火墙.360杀毒软件.电脑卫士等所有的电脑保护软件,避免在实验过程中攻击时被拒绝. 2.使用Windows获linux shell (1)在Wind ...
- 基础selenium+Python(定位、等待、打印)
1.第一个脚本 # coding = utf-8 from selenium import webdriver browser = webdriver.Firefox() browser.get(&q ...