我有這些表mysql的連接表查詢probleme
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)
);
,我試圖加入表,所以我得到了患者的名姓,他 診斷,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行
編輯:我會嘗試更好地解釋發生了什麼, 因此實例 我此行 西蒙Vermette在2014-03-04的房間2發作了心臟病發作,直到2014-03-07,2 和哮喘在房間4上2014-06-04直到2014-06-08
它會有一排 Simon Vermette壁爐攻擊2014-03-04 2014-03-07 Simon Vermette Asthma 2014-03-04 2014-03-07
西蒙Vermette爐攻擊2014年6月4日2014年6月8日 西蒙Vermette哮喘2014年6月4日2014年6月8日
所以,我想我以前有同樣的查詢,但我寫SELECT DISTINCT相反,我仍然有相同的表格 – Vermette 2014-11-22 06:35:24
爲所有表格提供小的示例值,您的輸出以及您想要的值。 – philipxy 2014-11-22 06:46:24