MySQL Query many-to-one join
I have a query that I am using that uses ID from several columns that refer to one table to get the information about that ID. Here is the code:
SEL开发者_如何转开发ECT `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()
What I want to do is capture the detail information about each ID. I cannot figure out how that is done. For example:
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.
If you change the JOIN
to
LEFT JOIN Associations ON
Associations.AssociationID IN (`Events`.AssociationID,
`Events`.Association2ID,
`Events`.Association3ID,
`Events`.Association4ID,
`Events`.Association5ID)
Does that improve things?
You probably also should consider restructuring things into first normal form.
Rather than having 5 columns reserved for potential AssociationID's the normal way of modelling this many to many relationship id to use a junction table with two columns AssociationID
and EventId
精彩评论