2010-12-09 88 views
0

我有一個查詢,我使用的是使用來自多個列的ID來引用一個表來獲取有關該ID的信息。這裏是代碼:MySQL Query多對一連接

SELECT `Events`.EventID, 
     `Events`.AssociationID, 
     `Events`.Association2ID, 
     `Events`.Association3ID, 
     `Events`.Association4ID, 
     `Events`.Association5ID, 
     `Events`.DateFrom, 
     `Events`.DateTo, 
     `Events`.EventName, 
     `Events`.VenueID, 
     `Events`.TestnTune, 
     `Events`.ShownShine, 
     `Events`.SpecialInfo, 
     `Events`.OtherInfo, 
     `Events`.Rating, 
     `Events`.EventOverlay, 
     `Events`.HavePictures, 
     `Events`.IncludeInSchedule, 
     `Events`.EventURL, 
     Associations.Active, 
     Associations.Acronym, 
     Associations.OrgName, 
     Associations.WebURL, 
     Associations.LogoURL, 
     Venues.LocationName, 
     Venues.Location, 
     Venues.longetude, 
     Venues.latitude, 
     Venues.Directions, 
     Venues.SitePros, 
     Venues.SiteCons, 
     Venues.BasicInfo, 
     Venues.SiteRating, 
     Venues.HostedEvents, 
     Venues.CurrentWeather 
    FROM `Events` 
LEFT JOIN Associations ON `Events`.AssociationID = Associations.AssociationID 
         AND `Events`.Association2ID = Associations.AssociationID 
         AND `Events`.Association3ID = Associations.AssociationID 
         AND `Events`.Association4ID = Associations.AssociationID 
         AND `Events`.Association5ID = Associations.AssociationID 
LEFT JOIN Venues ON `Events`.VenueID = Venues.VenueID 
    WHERE `Events`.DateFrom >= NOW() 

我想要做的是捕獲有關每個ID的詳細信息。我無法弄清楚這是如何完成的。例如:

For Association2ID: 
    Associations.Active, 
    Associations.Acronym, 
    Associations.OrgName, 
    Associations.WebURL, 
    Associations.LogoURL 

For Association3ID 
    Associations.Active, 
    Associations.Acronym, 
    Associations.OrgName, 
    Associations.WebURL, 
    Associations.LogoURL, 
etc. 
+2

的正常方式保留5列好了,你似乎是選擇這些列。你想成爲什麼?我看到你可能需要使用`或`而不是`和` - 並重新設計數據庫以將其放入第一個正常形式! – 2010-12-09 00:50:43

回答

0

如果更改JOIN

LEFT JOIN Associations ON 
Associations.AssociationID IN (`Events`.AssociationID, 
           `Events`.Association2ID, 
           `Events`.Association3ID, 
           `Events`.Association4ID, 
           `Events`.Association5ID) 

這是否改善的事情?

您可能還應該考慮重構爲first normal form

與其讓潛在AssociationID的模擬這種多對多的關係ID使用結合表有兩列AssociationIDEventId