简体   繁体   中英

MySQL Joins over 3 tables

Ok so I have a collection of tables and I am trying to create a statement that will return the module names that a certain member of staff teaches. I understand that joins are the most efficient way to attach these tables so that I can query them.

Staff

+---------+-------------+-----------------+
|Staff Id |Staff Name   |Grade            |
+---------+-------------+-----------------+
|E10010   |Alan Turing  |Senior Lecturer  |
|E10011   |Tony Hoare   |Reader           |
|E10012   |Seymour Cray |Lecturer         |
+---------+-------------+-----------------+

Teaches

+---------+-----------+
|Staff Id | Module Id |
+---------+-----------+
|E10010   |CS101      |
|E10011   |CS203      |
|E10012   |CS204      |
|E10010   |CS204      |
|E10011   |M101       |
|E10011   |CS101      |
+---------+-----------+

Module

+----------+-------------------------------+-------+
|Module Id |Module Name                    |Credits|
+----------+-------------------------------+-------+
|CS101     |Introduction to Computing      |10     |
|CS203     |Data Structures and Algorithms |10     |
|CS204     |Computer Architecture          |10     |
|M101      |Mathematics I                  |20     |
+----------+-------------------------------+-------+

Essentially, I want a query that looks something like:

SELECT Module Name FROM Staff, Teaches, Module WHERE Staff Id = 'E10010';

and this would return all the module names of the modules taught by Alan Turing:

+--------------------------+
|Introduction to Computing |
|Computer Architecture     |
+--------------------------+

I know that statement doesn't join the fields in the right way and that I need to use a JOIN statement, but am unsure about how to proceed with that. If someone could explain this, that would be great. Cheers in advance!

The query itself would be something like this:

SELECT ModuleName FROM module
JOIN teaches ON module.ModuleId = module.ModuleId
JOIN staff ON teaches.StaffId = staff.StaffId
WHERE staff.StaffId = "E10010"

You have to join both tables "teaches" and "staff", whereas the order matters! At first you need your "m:n"-connecting table "teaches" and establish the link between the "module" table and the "teaches"-table. After that, you join the "staff" table and got everything connected.

Be careful though that you have to prefix the attributes with the table name they belong to because they are ambiguous among the tables.

There are some caveats with joins and I would recommend this excellent ressource as a kickstart for learning them.

this should probably do it:

select `Module Name` from `Module` m
join `Teaches` t on t.`Module Id` = m.`Module id`
where t.`Staff Id` = `E10010`;

You need to use generated Ids, and preferably numeric, it would be faster. Here is a way you you can proceed with, using mysql:

create table staff (
   staff_id int auto_increment,
   staff_name varchar(32),
   grade varchar(32),
   PRIMARY KEY (staff_id)
)
create table module (
   module_id int auto_increment,
   module_name varchar(32) not null,
   PRIMARY KEY (module_id)
)
create table staff_module (
   staff_id int not null,
   module_id int not null,
   PRIMARY KEY (staff_id, module_id),
   CONSTRAINT `staff_module_fk_1` FOREIGN KEY staff_id REFERENCES staff(staff_id),
   CONSTRAINT `staff_module_fk_2` FOREIGN KEY module_id REFERENCES module(module_id)
)

and you can query as follows

select s.staff_name, m.module_name
from staff s, module m, staff_module r
where s.staff_id = r.staff_id
   and m.module_id = r.module_id

you can use the keyword join to rewrite the above query, but look it up online if u're interested.

note that syntax above is not tested.

hope this helps

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM