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 ...
随机推荐
- 【作业】Mental Rotation (模拟)
题目链接:https://vjudge.net/contest/345791#problem/L [问题描述] Mental rotation is a difficult thing to mast ...
- C++大数运算模板
#include<iostream> #include<cstring> #include<cstdio> #include<iomanip> #inc ...
- python selenium学习
selenium是一个python模块,具有自动化模拟浏览器的功能 selenium的中文文档地址:http://selenium-python-zh.readthedocs.io/en/latest ...
- [HTTPS] - 请求API失败(Could not create SSL/TLS secure channel)之解决
背景 在单元测试中请求 HTTPS API 失败. 异常 Result StackTrace: at System.Web.Services.Protocols.WebClientProtocol. ...
- python 之 数据库(多表查询之连接查询、子查询、pymysql模块的使用)
10.10 多表连接查询 10.101 内连接 把两张表有对应关系的记录连接成一张虚拟表 select * from emp,dep: #连接两张表的笛卡尔积 select * from emp,de ...
- Python字符串图解
>>> word = "Python" >>> word[:2] # character from the beginning to posi ...
- Linux和Windows系统目录结构区别
Windows目录结构图 Linux目录结构图 我们所有的操作尽量都要在/home/username目录下进行. 快捷进入家目录方式是cd ~.
- PB笔记之验证必填(pfc_validation)
pfc_validation事件中可以在保存时进行提示
- Spring依赖配置详解
<properties> <junit.version>4.12</junit.version> <spring.version>4.3.9.RELEA ...
- Thymeleaf模板引擎与springboot关联后,在html中无法使用el表达式获取model存的值
头部引入了thymeleaf <html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thy ...