mysql多对多关系的构建
来源:互联网 发布:极点五笔 centos 编辑:程序博客网 时间:2024/05/01 21:45
translate from http://www.phpknowhow.com/mysql/many-to-many-relationships/
在这篇文章中,我们要看看它有一个特殊的存储机制,多对多关系对多的关系。
也就是说,当两个表有多对多的关系,可以构造一个中间表来存储的关系,
看一下下面的`employee`和`'education`表, 不想翻译了。。。下面英文也不难。。。
id first_name last_name job_title salary notes
Think that Robin and Taylor hold BSc while Vivian holds both MSc and PhD. These relationships can be graphically represented like below.
You can see that for Vivian in `employee` table there are two related rows in `education` table and for BSc in `education` table there are two related rows in `employee` table forming many-to-many relationships (Each row in `employee` table can have more that one related row in `education` table and vise versa).
For storing many-to-many relationships, we need an intermediate table that mainly stores the primary keys (IDs) of each relationship. In this case, we can use a table (`employee_education`) like below.
Once relationships are stored, you can fetch data like below. In this query we fetch employee names and their education levels using SELECT statements and left joins.
1.
SELECT
emp.first_name, emp.last_name, edu.
name
AS
edu_name
FROM
`employee`
AS
emp
LEFT
JOIN
`employee_education`
AS
ee
ON
emp.id = ee.employee_id
LEFT
JOIN
`education`
AS
edu
ON
ee.education_id = edu.id;
+------------+-----------+----------+| first_name | last_name | edu_name |+------------+-----------+----------+| Robin | Jackman | BSc || Taylor | Edward | BSc || Vivian | Dickens | MSc || Vivian | Dickens | PhD || Harry | Clifford | NULL || Eliza | Clifford | NULL || Nancy | Newman | NULL || Melinda | Clifford | NULL || Harley | Gilbert | NULL |+------------+-----------+----------+
You can omit the employees whose education levels are not set by having a WHERE clause.
1.
SELECT
emp.first_name, emp.last_name, edu.
name
AS
edu_name
FROM
`employee`
AS
emp
LEFT
JOIN
`employee_education`
AS
ee
ON
emp.id = ee.employee_id
LEFT
JOIN
`education`
AS
edu
ON
ee.education_id = edu.id
WHERE
edu.
name
IS
NOT
NULL
;
+------------+-----------+----------+| first_name | last_name | edu_name |+------------+-----------+----------+| Robin | Jackman | BSc || Taylor | Edward | BSc || Vivian | Dickens | MSc || Vivian | Dickens | PhD |+------------+-----------+----------+
- mysql多对多关系的构建
- MySql多对多关系中外键的应用
- mysql多对多的关系如何表示(主键也可做外键)
- MySQL数据库通过navicat建立多对多关系
- mysql 对cpu core 的依赖关系
- 关系数据库—多对多的关系
- Hibernate之多对多关系之间的关系映射
- 多对多关系
- 多对多关系
- 多对多关联关系的使用
- Hibernate的多对多关联关系
- Hibernate的关系映射多对多
- 双向多对多关系的映射
- hibernate实现多对多的关系
- thinkphp的多对多关系操作
- Hibernate,多对多的关联关系
- 多对多的关系操作
- 范例讲解:多对多的关系
- 这酸爽,VS2012与cuda6.5的安装与配置
- [HDU3401]Trade && 单调队列优化DP
- WIN8 与WIN7的64位及32位 分别对Legacy BIOS+MBR和UEFI+GPT两种启动方式和分区架构下的安装可行性分析
- unity之继承的复习和多态的复习还有抽象类
- 设计模式:原型模式
- mysql多对多关系的构建
- RACCommand 有sendError对executionSignals 怎么破
- weka中文使用说明(二)
- java语言编程:求两个字符串的最大子串
- HDU 1856 More is better (并查集)
- 电子游戏与社会——课程笔记
- POJ 题目2184 Cow Exhibition(背包变形)
- unbuntu14.04下全志(蜂鸟)A31编译SDK全过程
- weka中文使用说明(三)