如何在两列上有效地查询多对多关系

时间:2013-11-03 18:01:49

标签: mysql sql many-to-many

我创建了这三个表,代表了医生和医生专业之间的多对多关系。

医生可以有很多专业,很多医生都有一定的专长。

CREATE TABLE doctors (
  doctor_id int(11) NOT NULL AUTO_INCREMENT,
  firstname varchar(30) NOT NULL,
  lastname varchar(30) NOT NULL,
  area varchar(50) NOT NULL,
  PRIMARY KEY (doctor_id)
);

CREATE TABLE has_specialty (
  doctor_id int(11) NOT NULL,
  specialty_id int(11) NOT NULL,
  PRIMARY KEY (doctor_id,specialty_id),
  FOREIGN KEY (doctor_id) REFERENCES doctors (doctor_id),
  FOREIGN KEY (specialty_id) REFERENCES specialties (specialty_id)
);

CREATE TABLE IF specialties (
  specialty_id int(11) NOT NULL AUTO_INCREMENT,
  specialty varchar(254) NOT NULL UNIQUE,
  PRIMARY KEY (specialty_id)
);
  • 我想要的是找到所有拥有专业“H”且位于“B”区域的医生。

例如,假设我们有这个数据库:

DOCTORS
+-----------+-----------+----------+----------+
| doctor_id | firstname | lastname | area     |
+-----------+-----------+----------+----------+
|         1 | Virginia  | Clark    | A        |
|         2 | Jane      | Brown    | B        |
|         3 | Adam      | Harris   | D        |
|         4 | Anthony   | Rogers   | D        |
|         5 | Paula     | Lopez    | B        |
|         6 | Jerry     | Howard   | A        |
|         7 | Willie    | Nelson   | C        |
|         8 | Juan      | Perry    | A        |
|         9 | Victor    | Allen    | B        |
+-----------+-----------+----------+----------+

SPECIALTIES
+--------------+-----------+
| specialty_id | specialty |
+--------------+-----------+
| 1            | A         |
| 2            | B         |
| 3            | C         |
| 4            | D         |
| 5            | E         |
| 6            | F         |
| 7            | G         |
| 8            | H         |
| 9            | I         |
+--------------+-----------+

HAS_SPECIALTY
+-----------+--------------+
| doctor_id | specialty_id |
+-----------+--------------+
| 1         | 4            |
| 1         | 6            |
| 1         | 8            |
| 2         | 3            |
| 2         | 8            |
| 3         | 1            |
| 3         | 4            |
| 3         | 5            |
| 4         | 4            |
| 5         | 8            |
| 5         | 9            |
| 6         | 2            |
| 6         | 7            |
| 7         | 9            |
| 8         | 4            |
| 9         | 2            |
| 9         | 3            |
+-----------+--------------+

结果应为:

+-----------+-----------+----------+----------+
| doctor_id | firstname | lastname | area     |
+-----------+-----------+----------+----------+
|         2 | Jane      | Brown    | B        |
|         5 | Paula     | Lopez    | B        |
+-----------+-----------+----------+----------+

1 个答案:

答案 0 :(得分:0)

你可以用简单的INNER JOIN

来做到这一点
SELECT d.doctor_id, d.firstname,d.lastname,d.area     
FROM doctors d
INNER JOIN has_specialty hs ON d.doctor_id = hs.doctor_id
INNER JOIN specialties s ON hs.specialty_id = s.specialty_id
WHERE s.specialty = 'H' AND d.area = 'B';

sqlfiddle demo