mysql多对多关系的构建

translate from http://www.phpknowhow.com/mysql/many-to-many-relationships/


在这篇文章中,我们要看看它有一个特殊的存储机制,多对多关系对多的关系。


也就是说,当两个表有多对多的关系,可以构造一个中间表来存储的关系,


看一下下面的`employee`和`'education`表, 不想翻译了。。。下面英文也不难。。。


idfirst_namelast_namejob_titlesalarynotes
1RobinJackmanSoftware Engineer5500 
2TaylorEdwardSoftware Architect7200 
3VivianDickensDatabase Administrator6000 
4HarryCliffordDatabase Administrator6800 
5ElizaCliffordSoftware Engineer4750 
6NancyNewmanSoftware Engineer5100 
7MelindaCliffordProject Manager8500 
8HarleyGilbertSoftware Architect8000 
idname
1BSc
2MSc
3PhD

Think that Robin and Taylor hold BSc while Vivian holds both MSc and PhD. These relationships can be graphically represented like below.

Many-to-many relationships

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.

employee_ideducation_id
11
21
32
33

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      |
+------------+-----------+----------+
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值