一个对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 ...
随机推荐
- CF666E Forensic Examination SAM+线段树合并+前缀树倍增
$ \color{#0066ff}{ 题目描述 }$ 给你一个串\(S\)以及一个字符串数组\(T[1..m]\),\(q\)次询问,每次问\(S\)的子串\(S[p_l..p_r]\)在\(T[l. ...
- C/C++入门易错点及常用小技巧
int型:绝对值在10^9范围内的整数都可以定义为int型 long long 型:如果long long型赋值大于2^23-1的初值,需要在初值后面加LL,否则会编译错误. float,double ...
- SpringCloud与Docker微服务架构实战笔记
一 微服务架构概述 1. 单体应用架构存在的问题 结合:https://www.cnblogs.com/jialanshun/p/10637454.html一起看,在该篇博客中搜索“单块架构的优缺点 ...
- mysql 查询json字段 json_extract (mysql 5.7及以上)
找第一层: SELECT * FROM tourists WHERE json_data->'$.weixinOpenId' = '299485886686868' 或者 SELECT * FR ...
- 换个角度看Salesforce之基础配置学习笔记(二)
1. 登录后无法使用Developer Console? 先找到当前登录用户的Profie,然后勾选Profile中的View All Data(Modify All Data)即可: 2. Pers ...
- myEclipse卡顿解决
1 .关闭MyEclipse的自动validation windows > perferences > myeclipse > validation 将Build下全部勾取消 如果你 ...
- SpringBoot + Quartz定时任务示例
程序文件结构,如下图,后面详细列出各文件的代码: 1. maven的pom.xml文件如下: <project xmlns="http://maven.apache.org/POM/4 ...
- 新建IP核为灰色并显示there is no project open
问题: ise显示there is no project open. “You may browse the IP Catalog but you will not be able to genera ...
- [Android]Caused by: java.lang.IllegalArgumentException: Service not registered.md
Caused by: java.lang.IllegalArgumentException: Service not registered: org.diql.aidldemo.MainActivit ...
- 【debian】给用户添加sudo权限
新装的debian系统默认是没有sudo功能的. 于是,在root用户权限下: apt-get install sudo 然后再修改文件 /etc/sudoers : chmod +w /etc/su ...