mysql连接表查询问题

时间:2014-11-22 04:56:42

标签: mysql

我有那些表

create table patient(
PatientId integer not null,
FirstName varchar(15) not null,
LastName  varchar(15) not null,
Address varchar(40) not null,
DateOfBirth date not null,
Gender varchar(1) not null,
Height varchar(10),
Weight varchar(10),
Phone varchar(20) not null,
PRIMARY KEY(PatientId) 
);

create table room(
RoomNo integer  not null,
Type varchar(25) not null,
NumberOfBeds numeric(1) not null,
PRIMARY KEY(RoomNo)
);



create table diagnostic(
   DiagnosticId integer not null,
   Description varchar(25) not null,
   PRIMARY KEY(DiagnosticId)
   );

   create table patientRoom(
   PatientId integer not null,
   RoomNo integer not null,
   DateStart date not null,
   DateEnd date not null,
   PRIMARY KEY(PatientId, RoomNo, DateStart),
   FOREIGN KEY(PatientId) REFERENCES patient(PatientId),
   FOREIGN KEY(RoomNo) REFERENCES room(RoomNo)
   );

   create table patientDiagnostic(
   PatientId integer not null,
   DiagnosticId integer not null,
   Date date not null,
   PRIMARY KEY(PatientId,DiagnosticId),
   FOREIGN KEY(PatientId) REFERENCES patient(PatientId),
   FOREIGN KEY(DiagnosticId) REFERENCES diagnostic(DiagnosticId)
   );

我正在尝试加入该表,因此我得到了患者的FirstName LastName,以及他的 诊断,RoomNo,包括他来的日期和他离开的日期

我试过这个

select p.FirstName, p.LastName,
d.Description,
pr.RoomNo, pr.DateStart, pr.DateEnd
from patient p, patientRoom pr, diagnostic d, patientDiagnostic pd
where p.PatientId = pd.PatientId
and 
pd.PatientId = pr.PatientId
and
pd.DiagnosticId = d.DiagnosticId;

但是我获得了361行而不是245行 我认为错误在于:

where p.PatientId = pd.PatientId
and
pd.PatientId = pr.PatiendId.

但我不知道如何解决它。 像一些患者在不同的日期得到3种不同的诊断 但是那些我得到9行而不是3行

编辑:我会尝试更好地解释发生了什么, 所以例子 我有这一排 Simon Vermette于2014-03-04至2014-03-07在2号房间心脏病发作, 2014-06-04至2014-06-08第4室的哮喘和哮喘

它会有一排 西蒙Vermette壁炉攻击2014-03-04 2014-03-07 Simon Vermette哮喘2014-03-04 2014-03-07

Simon Vermette壁炉袭击2014-06-04 2014-06-08 Simon Vermette哮喘2014-06-04 2014-06-08

4 个答案:

答案 0 :(得分:0)

尝试

SELECT DISTINCT ...

如果没有DISTINCT,WHERE将从每个FROM表的行的所有可能组合中进行过滤。在此之后使用DISTINCT删除重复项。

答案 1 :(得分:0)

试试这个:

select p.FirstName, p.LastName,
d.Description,
pr.RoomNo, pr.DateStart, pr.DateEnd
from patient p LEFT JOIN  patientRoom pr ON (pr.PatientId = p.PatientId)
LEFT JOIN patientDiagnostic pd ON  (pd.PatientId = p.PatientId)
LEFT JOIN  diagnostic d ON (d.DiagnosticId = pd.DiagnosticId)

答案 2 :(得分:0)

这个查询怎么样?

select p.FirstName, p.LastName,
d.Description,
pr.RoomNo, pr.DateStart, pr.DateEnd
from patient p, patientRoom pr, diagnostic d, patientDiagnostic pd
where p.PatientId = pd.PatientId
and 
p.PatientId = pr.PatientId
and
pd.DiagnosticId = d.DiagnosticId;

答案 3 :(得分:0)

试试这个..

 SELECT p.FirstName, p.LastName, d.Description, pr.RoomNo, pr.DateStart, pr.DateEnd
 FROM patient p
 LEFT JOIN patientroom pr ON ( pr.PatientId = p.PatientId )
 LEFT JOIN patientdiagnostic pd ON ( pd.PatientId = p.PatientId )
 LEFT JOIN diagnostic d ON ( d.DiagnosticId = pd.DiagnosticId )
 GROUP BY p.FirstName, p.LastName, d.Description
相关问题