2017-02-10 89 views
0

查找這是我的查詢:MySQL的 - 結構與不同價值觀的查詢,需要從同一個表

SELECT f.pending 
     , f.title 
     , f.funeraltype 
     , f.firstname 
     , f.middlename 
     , f.lastname 
     , f.suffix 
     , f.displayage 
     , f.age 
     , f.displaycity 
     , f.city 
     , f.displaydate 
     , f.date 
     , f.aftersunset 
     , f.funeraldate 
     , f.displayfuneraldate 
     , f.ftime 
     , f.displayservicelocation 
     , l.location 
     , f.displaycemetery 
     , c.name 
     , f.displayobituary 
     , f.obituary 
     , f.displaycharity1 
     , ch.name 
     , f.charity1memo 
     , f.displaycharity2 
     , f.charity2 
     , f.charity2memo 
     , f.displaycharity3 
     , f.charity3 
     , f.charity3memo 
     , f.displaycharity4 
     , f.charity4 
     , f.charity4memo 
     , f.displaycharity5 
     , f.charity5 
     , f.charity5memo 
     , f.memorialbook 
     , f.displayshiva 
     , f.displaytime 
     , f.shivaendpart 
     , f.shivauntil 
     , f.shivaatresidence 
     , f.shivaname 
     , f.shivaaddress 
     , f.shivacity 
     , f.shivastate 
     , f.shivazip 
     , f.shivaphone 
     , f.shivacomment 
     , f.displayfamilymembers 
     , f.familymembers 
     , f.displayclergy 
     , f.clergy 
     , f.livevideo 
     , f.archivevideo 
     , f.video 
     , f.usemp4video 
     , f.mp4video 
     , f.custom_text 
     , f.charities_footnote 
     , f.charity1note 
     , f.charity2note 
     , f.charity3note 
     , f.charity4note 
     , f.charity5note 
    FROM funerals f 
     , locations l 
     , cemeteries c 
     , charities ch 
    WHERE f.servicelocation = l.locationid 
    AND f.cemetery = c.cemeteryID 
    AND f.charity1 = ch.CharityID 
    AND f.charity2 = ch.CharityID 
    AND f.charity3 = ch.CharityID 
    AND f.charity4 = ch.CharityID 
    AND f.charity5 = ch.CharityID 

ch.name就是我要找來提取。 f.charity1,f.charity2,f.charity3,f.charity4,& f.charity5都包含需要在「慈善機構」表查找不同的ID。我將如何構造這個查詢來顯示每個慈善機構的名字?

+1

我強烈建議你考慮大幅修改您的架構設計 – Strawberry

+0

是 - 這就是爲什麼我需要得到的數據出來的這首分貝。 – MG1

+1

所以看看JOINs。然後將慈善表加入葬禮5次,每次爲慈善組織一次,並且記住爲每個聯合表的實例分配自己的別名。我們在1992年左右停止使用逗號連接,當時它們被有效地棄用。 – Strawberry

回答

0

試試這個:

SELECT 
     f.pending 
     , f.title 
     , f.funeraltype 
     , f.firstname 
     , f.middlename 
     , f.lastname 
     , f.suffix 
     , f.displayage 
     , f.age 
     , f.displaycity 
     , f.city 
     , f.displaydate 
     , f.date 
     , f.aftersunset 
     , f.funeraldate 
     , f.displayfuneraldate 
     , f.ftime 
     , f.displayservicelocation 
     , l.location 
     , f.displaycemetery 
     , c.name 
     , f.displayobituary 
     , f.obituary 
     , f.displaycharity1 
     , ch1.name 
     , ch2.name 
     , ch3.name 
     , ch4.name 
     , ch5.name 
     , f.charity1memo 
     , f.displaycharity2 
     , f.charity2 
     , f.charity2memo 
     , f.displaycharity3 
     , f.charity3 
     , f.charity3memo 
     , f.displaycharity4 
     , f.charity4 
     , f.charity4memo 
     , f.displaycharity5 
     , f.charity5 
     , f.charity5memo 
     , f.memorialbook 
     , f.displayshiva 
     , f.displaytime 
     , f.shivaendpart 
     , f.shivauntil 
     , f.shivaatresidence 
     , f.shivaname 
     , f.shivaaddress 
     , f.shivacity 
     , f.shivastate 
     , f.shivazip 
     , f.shivaphone 
     , f.shivacomment 
     , f.displayfamilymembers 
     , f.familymembers 
     , f.displayclergy 
     , f.clergy 
     , f.livevideo 
     , f.archivevideo 
     , f.video 
     , f.usemp4video 
     , f.mp4video 
     , f.custom_text 
     , f.charities_footnote 
     , f.charity1note 
     , f.charity2note 
     , f.charity3note 
     , f.charity4note 
     , f.charity5note 
FROM funerals f 
INNER JOIN locations l on f.servicelocation = l.locationid 
INNER JOIN cemeteries c ON f.cemetery = c.cemeteryID 
INNER JOIN charities ch1 ON f.charity1 = ch1.CharityID 
INNER JOIN charities ch2 ON f.charity2 = ch2.CharityID 
INNER JOIN charities ch3 ON f.charity3 = ch3.CharityID 
INNER JOIN charities ch4 ON f.charity4 = ch4.CharityID 
INNER JOIN charities ch5 ON f.charity5 = ch5.CharityID 
1
SELECT f.pending 
     , l.location 
     , f.displaycemetery 
     , c.name 
     , f.displaycharity1 
     , ch.name 
     , f.charity1memo 
     , f.displaycharity2 
     , f.charity2 
     , f.charity2memo 
     , f.displaycharity3 
     , f.charity3 
     , f.charity3memo 
     , f.displaycharity4 
     , f.charity4 
     , f.charity4memo 
     , f.displaycharity5 
     , f.charity5 
     , f.charity5memo 
     , f.charity1note 
     , f.charity2note 
     , f.charity3note 
     , f.charity4note 
     , f.charity5note 
    FROM funerals f 
    JOIN locations l 
    ON l.locationid = f.servicelocation 
    JOIN cemeteries c 
    ON c.cemeteryID = f.cemetery 
    JOIN charities ch1 
    ON ch1.CharityID = f.charity1 
    JOIN charities ch2 
    ON ch2.CharityID = f.charity2 
    etc. 
相關問題