2014-11-22 41 views
0

我有這些表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日

+0

所以,我想我以前有同樣的查詢,但我寫SELECT DISTINCT相反,我仍然有相同的表格 – Vermette 2014-11-22 06:35:24

+0

爲所有表格提供小的示例值,您的輸出以及您想要的值。 – philipxy 2014-11-22 06:46:24

回答

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 
+0

okok它更接近,我現在有245行像我應該,但它仍然給每個患者誰擁有相同的日期和空間多於一個診斷。反正謝謝,我會去睡覺。我明天要去檢查一下。謝謝 – Vermette 2014-11-22 06:58:26

+0

我明白了你的觀點...我修改了查詢,現在試試這個 SELECT p.FirstName,p.LastName,d.Description,pr.RoomNo,pr.DateStart,pr.DateEnd 從患者p LEFT JOIN patientroom PR ON(pr.PatientId = p.PatientId) LEFT JOIN patientdiagnostic PD ON(pd.PatientId = p.PatientId) RIGHT JOIN診斷d ON(d.DiagnosticId = pd.DiagnosticId) AND pd.Date = PR。 DateStart GROUP BY p.FirstName,p.LastName,d。內容提要 – 2014-11-22 07:26:31

+0

okok酷它正在工作,但除了它是刪除2行,我要檢查哪些行被刪除,看看有什麼問題,謝謝 – Vermette 2014-11-22 18:30:11

0

嘗試

SELECT DISTINCT ... 

沒有區別於所有WHERE過濾器來自每個FROM表格的行的可能組合。之後使用DISTINCT刪除重複項。

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) 
+0

仍然有同樣的問題,thx tho – Vermette 2014-11-22 06:18:52

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; 
+0

仍然有同樣的問題,thx反正 – Vermette 2014-11-22 06:18:19