MySQL之三张表关联
创建三张表
1、学生表
mysql> create table students(
sid int primary key auto_increment,
sname varchar(100) not null,
age int,
address varchar(100),
courseid int,
constraint fk_stu_cid foreign key(courseid)
references course(cid)
);
mysql> insert into students(sname,age,address,courseid) values('小海子',23,'北京',1003);
mysql> insert into students(sname,age,address,courseid) values('小沈阳',45,'沈阳',1003);
mysql> insert into students(sname,age,address,courseid) values('刘阳',25,'山东',1002);
mysql> insert into students(sname,age,address,courseid) values('甘能',22,'广东',1002);
mysql> select * from students;
+-----+--------+-----+---------+----------+
| sid | sname | age | address | courseid |
+-----+--------+-----+---------+----------+
| 1 | 小海子 | 23 | 北京 | 1003 |
| 2 | 小沈阳 | 45 | 沈阳 | 1003 |
| 3 | 刘阳 | 25 | 山东 | 1002 |
| 4 | 甘能 | 22 | 广东 | 1002 |
+-----+--------+-----+---------+----------+
2、老师表
mysql> create table teacher(
-> tid int(5) primary key auto_increment,
-> tname varchar(100) not null,
-> age int(4),
-> address varchar(100),
-> courseid int
-> )engine=innodb auto_increment=101;
mysql> insert into teacher(tname,age,address) values('马云',50,'杭州');
mysql> insert into teacher(tname,age,address) values('赵本山',52,'沈阳');
mysql> insert into teacher(tname,age,address) values('刘强东',45,'北京');
mysql> select* from teacher;
+-----+--------+-----+---------+----------+
| tid | tname | age | address | courseid |
+-----+--------+-----+---------+----------+
| 101 | 马云 | 50 | 杭州 | NULL |
| 102 | 赵本山 | 52 | 沈阳 | NULL |
| 103 | 刘强东 | 45 | 北京 | NULL |
+-----+--------+-----+---------+----------+
3、课程表
mysql> create table course(
-> cid int primary key auto_increment,
-> cname varchar(100) not null,
-> xuefen int,
-> tid int,
-> constraint fk_course_tid foreign key(tid)
-> references teacher(tid)
-> )engine=innodb auto_increment = 1001;
mysql> insert into course(cname,xuefen,tid) values('C++',3,'');
mysql> insert into course(cname,xuefen,tid) values('java',5,'');
mysql> insert into course(cname,xuefen,tid) values('相声表演',2,'');
mysql> insert into course(cname,xuefen,tid) values('电子商务',3,'');
mysql> select * from course;
+------+----------+--------+-----+
| cid | cname | xuefen | tid |
+------+----------+--------+-----+
| 1001 | C++ | 3 | 101 |
| 1002 | java | 5 | 101 |
| 1003 | 相声表演 | 2 | 102 |
| 1004 | 电子商务 | 3 | 103 |
+------+----------+--------+-----+
学生修了哪些课程
mysql> select s.sname,c.cid,c.cname
-> from students s left join course c
-> on s.courseid = c.cid;
+--------+------+----------+
| sname | cid | cname |
+--------+------+----------+
| 刘阳 | 1002 | java |
| 甘能 | 1002 | java |
| 小海子 | 1003 | 相声表演 |
| 小沈阳 | 1003 | 相声表演 |
+--------+------+----------+
学生修的课程有哪些老师教
mysql> select s.sname,c.cid,c.cname,t.tname
-> from students s,course c,teacher t
-> where s.courseid = c.cid and c.tid = t.tid;
+--------+------+----------+--------+
| sname | cid | cname | tname |
+--------+------+----------+--------+
| 刘阳 | 1002 | java | 马云 |
| 甘能 | 1002 | java | 马云 |
| 小海子 | 1003 | 相声表演 | 赵本山 |
| 小沈阳 | 1003 | 相声表演 | 赵本山 |
+--------+------+----------+--------+
或者
mysql> select s.sname,c.cid,c.cname,t.tname
-> from students s inner join course c inner join teacher t
-> on s.courseid = c.cid and c.tid = t.tid; +--------+------+----------+--------+
| sname | cid | cname | tname |
+--------+------+----------+--------+
| 刘阳 | 1002 | java | 马云 |
| 甘能 | 1002 | java | 马云 |
| 小海子 | 1003 | 相声表演 | 赵本山 |
| 小沈阳 | 1003 | 相声表演 | 赵本山 |
+--------+------+----------+--------+
其他关联不行。left join ,right join
2018年1月17日01:07:40
MySQL之三张表关联的更多相关文章
- mysql三张表关联查询
三张表,需要得到的数据是标红色部分的.sql如下: select a.uid,a.uname,a.upsw,a.urealname,a.utel,a.uremark, b.rid,b.rname,b. ...
- MySQL 两张表关联更新(用一个表的数据更新另一个表的数据)
有两张表,info1, info2 . info1: info2: 现在,要用info2中的数据更新info1中对应的学生信息,sql语句如下: UPDATE info1 t1 JOIN info2 ...
- 【Mysql进阶技巧(1)】 MySQL的多表关联与自连接
自连接 测试数据准备 CREATE TABLE `t2` ( `id` int(11) NOT NULL, `gid` char(1) DEFAULT NULL, `col1` int(11) DEF ...
- MSSQL N张表关联查询
declare @newTime varchar(50); declare @lasetTime varchar(50); set @newTime= getdate(); set @lasetTim ...
- Oracle两张表关联批量更新其中一张表的数据
Oracle两张表关联批量更新其中一张表的数据 方法一(推荐): UPDATE 表2 SET 表2.C = (SELECT B FROM 表1 WHERE 表1.A = 表2.A) WHERE EXI ...
- mysql一张表到底能存多少数据?
前言 程序员平时和mysql打交道一定不少,可以说每天都有接触到,但是mysql一张表到底能存多少数据呢?计算根据是什么呢?接下来咱们逐一探讨 知识准备 数据页 在操作系统中,我们知道为了跟磁盘交互, ...
- Mysql两张表的关联字段不一致
工作中遇到了一个问题,邮件系统群发失败,后来经过排查查找到了原因 原来是因为mysql中的两张表的关联字段竟然不一致, 表A mysql> desc rm_user_router;+------ ...
- mysql一张表多个字段关联另一张表查询
如下:一张订单表多个字段关联用户表: 1.链表查询 SELECT cu.id AS 'id',cu.version AS 'version',cu.cid AS 'cid',cu.uid AS 'ui ...
- Oracle中如何实现Mysql的两表关联update操作
在看<MySQL 5.1参考手册>的时候,发现MySQL提供了一种两表关联update操作.原文如下: UPDATE items,month SET items.price=month.p ...
随机推荐
- 虚拟机中Linux环境下使用Squid部署代理缓存服务(及透明传输)
小知识: 正确的使用Squid服务程序部署代理缓存服务可以有效提升访问静态资源的效率,降低原服务器的负载. 不仅如此,还为读者们添加了对指定IP地址.网页关键词.网址与文件后缀的ACL访问限制功能的实 ...
- LeetCode 108. 将有序数组转换为二叉搜索树(Convert Sorted Array to Binary Search Tree) 14
108. 将有序数组转换为二叉搜索树 108. Convert Sorted Array to Binary Search Tree 题目描述 将一个按照升序排列的有序数组,转换为一棵高度平衡二叉搜索 ...
- 032 Android Service
1.介绍 2.新建Service (1) (2)在Androidmanifest.xml文件中配置service <service android:name=".Myservice&q ...
- MySQL中的case when 中对于NULL值判断的坑
sql中的case when 有点类似于Java中的switch语句,比较灵活,但是在Mysql中对于Null的处理有点特殊 Mysql中case when语法: 语法1: CASE case_val ...
- Spring Boot使用@ConfigurationProperties注解获取配置文件中的属性值
注意:这种方式要提供属性的getter/setter方法—— 如果idea报错,提示没有相应的执行器,就需要在maven中添加: (虽然不配置代码也能正常运行,作用在下面会说明) 配置了该执行器后,在 ...
- ArcGIS SOE开发异常之 ClassFactory cannot supply requested class
最近SOE开发一个功能,辛辛苦苦写完, 异常: ClassFactory cannot supply requested class 辛苦解决: 百度一下,描述这个问题的帖子很多,不过内容基本一致.大 ...
- [BZOJ4755][JSOI2016]扭动的回文串(manacher+Hash)
前两种情况显然直接manacher,对于第三种,枚举回文中心,二分回文半径,哈希判断即可. #include<cstdio> #include<algorithm> #defi ...
- VMWare linux虚拟机(centos没有GUI)联网(NAT模式)
使用yum list命令查看是否能连上网. 不能联网,需要对centos进行网络配置.但在此之前,需要: 1. 虚拟机网络连接方式设置成NAT. 2. window系统下的两个服务VMwareDHCP ...
- php 判断请求是否是json
$object =file_get_contents("php://input"); $arr = is_json($object); if($arr){ var_dump($ar ...
- Linux文件属性整理
Linux系统是一种典型的多用户系统,不同的用户处于不同的地位,拥有不同的权限.为了保护系统的安全性,Linux系统对不同的用户访问同一文件(包括目录文件)的权限做了不同的规定.在Linux中我们可以 ...