Inner Join 3 tables

2019-09-04 11:23发布

I have 3 tables and I want to know how can I do a triple join with this type of setup if at all?

First Table: LOCATIONS_TABLE
locationID
websiteID
locationCity
locationState
locationCountry
locationURL

Second Table: PREF_TABLE
pref_ID
Pref_LocationID
Pref_WebsiteID
Pref_SavedTitle

Third Table: WEBSITECATEGORY_TABLE
wbcatID
websiteID
WBLinkCategoryParentID
WBLinkTitle
WBLinkURL

the 2nd table is where the user has a store preference with a Pref_SavedTitle for their Locations they have saved for later reference.

The Current SQL Statement I have which only does a double inner join is...

SELECT 
LOCATIONS_TABLE.LocationWebsiteID, 
LOCATIONS_TABLE.locationCity, 
LOCATIONS_TABLE.locationState, 
LOCATIONS_TABLE.locationCountry, 
LOCATIONS_TABLE.locationURL, 
PREF_TABLE.Pref_SavedTitle 
FROM PREF_TABLE INNER JOIN LOCATIONS_TABLE 
ON PREF_TABLE.Pref_LocationID = LOCATIONS_TABLE.LocationID 
WHERE PREF_TABLE.Pref_SavedTitle = 'AlabamaPREF'

This returns sample data of...

LocationWebsiteID = 2 
locationCity = Mobile 
locationState = Alabama 
locationCountry = United States
locationURL = alabama.bmv.org
Pref_SavedTitle - AlabamaPREF 

The 3rd table has the sample data of...

wbcatID = 1
websiteID = 2
WBLinkCategoryParentID = 0
WBLinkTitle = Alabama Resources
WBLinkURL = /alabama-resources

This does exactly what it needs to do which is returns me all of the LOCATION_TABLE items that equal the LocationID that was stored previously in the PREF_TABLE.

However I have a 3rd table that I need to add to this equation (Which is the Third Table) listed above. I need to be able to link the Third Table on the websiteID somehow so that it knows the WBLinkTitle and WBLinkURL are associated with that specific websiteID

On the final output... I need to be able to pull the results for the columns...

LOCATIONS_TABLE.websiteID, 
LOCATIONS_TABLE.locationURL, 
WEBSITECATEGORY_TABLE.WBLinkTitle, 
WEBSITECATEGORY_TABLE.WBLinkURL

which based off my sample data would be...

websiteID = 2
locationURL = alabama.dmv.org
WBLinkTitle = Alabama Resources
WBLinkURL = /alabama-resources

SQLFIDDLE EXAMPLE

1条回答
疯言疯语
2楼-- · 2019-09-04 12:23

UPDATED v-1

I've updated the answer according to your comment (now the 3rd table is inner-joined to PREF_TABLE and additional condition was added to the first join: AND PREF_TABLE.Pref_WebsiteID = LOCATIONS_TABLE.LocationWebsiteID to prevent all-locations loading):

SELECT 
    LOCATIONS_TABLE.LocationWebsiteID, 
    LOCATIONS_TABLE.locationCity, 
    LOCATIONS_TABLE.locationState, 
    LOCATIONS_TABLE.locationCountry, 
    LOCATIONS_TABLE.locationURL, 
    PREF_TABLE.Pref_SavedTitle,
    WEBSITECATEGORY_TABLE.WBLinkTitle,
    WEBSITECATEGORY_TABLE.WBLinkURL
FROM 
    PREF_TABLE 
    INNER JOIN LOCATIONS_TABLE 
        ON PREF_TABLE.Pref_LocationID = LOCATIONS_TABLE.LocationID
           AND PREF_TABLE.Pref_WebsiteID = LOCATIONS_TABLE.LocationWebsiteID
    INNER JOIN WEBSITECATEGORY_TABLE 
        ON WEBSITECATEGORY_TABLE.websiteID = PREF_TABLE.Pref_WebsiteID 
WHERE 
    PREF_TABLE.Pref_SavedTitle = 'AlabamaPREF'
查看更多
登录 后发表回答