多对多表JOIN

时间:2013-02-13 13:46:13

标签: mysql join relational-database

我正试图在患者和护士之间建立多对多的关系。

我需要将PatientID分配给NurseID;所以病人和护士之间有关系。这可以通过函数TABLE JOIN吗?

到目前为止,我有三张桌子:

Patient 
(
PatientID (PK), 
Forename, 
surname, 
gender, 
date of birth, 
address, 
illness, 
prioirty
)

seen_by 
(
ID(PK),
PatientID(FK), 
NurseID(FK)
) 

Nurse 
(
NurseID(PK),  
Forename, 
surname
)

我在seen_by表中添加了一个外国约束,以增加患者和护士之间的关系。

Patient (PatientID (PK), Forename, surname, gender, date of birth, address, illness, prioirty)

seen_by (ID(PK) PatientID(FK to Patient.PatientID), NurseID(FK to Nurse.NurseID) )

Nurse (NurseID(PK), Forename, surname)

对我做错的任何建议?谢谢你提前!

1 个答案:

答案 0 :(得分:1)

试试这个:

select p.*,s.*,n.* 
from patient as p inner join seen_by as s 
on p.PatientID=s.PatientID
inner join Nurse as n 
on n.NurseID=s.NurseID

如果需要,可以添加最后的位置

select p.*,s.*,n.* 
from patient as p inner join seen_by as s 
on p.PatientID=s.PatientID
inner join Nurse as n 
on n.NurseID=s.NurseID
where p.field='dumbtext'  -- you can add here the rest of the conditions