我有3个表格,我想知道如果有这种类型的设置我怎样才能进行三重连接?
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
第二个表是用户具有商店首选项的地方,其中包含Pref_SavedTitle,用于保存以供日后参考的位置。
我只有双内连接的当前SQL语句是......
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'
返回......
的样本数据LocationWebsiteID = 2
locationCity = Mobile
locationState = Alabama
locationCountry = United States
locationURL = alabama.bmv.org
Pref_SavedTitle - AlabamaPREF
第3张表的样本数据为......
wbcatID = 1
websiteID = 2
WBLinkCategoryParentID = 0
WBLinkTitle = Alabama Resources
WBLinkURL = /alabama-resources
这正是它需要做的事情,它会返回所有LOCATION_TABLE项目,这些项目等于先前存储在PREF_TABLE中的LocationID。
但是,我需要在第3个表中添加上面列出的等式(即第三个表)。我需要能够以某种方式链接网站ID上的第三个表,以便它知道WBLinkTitle和WBLinkURL与该特定网站ID相关联
在最终输出中......我需要能够为列提取结果......
LOCATIONS_TABLE.websiteID,
LOCATIONS_TABLE.locationURL,
WEBSITECATEGORY_TABLE.WBLinkTitle,
WEBSITECATEGORY_TABLE.WBLinkURL
基于我的样本数据的将是......
websiteID = 2
locationURL = alabama.dmv.org
WBLinkTitle = Alabama Resources
WBLinkURL = /alabama-resources
答案 0 :(得分:5)
更新v-1
我已根据您的评论更新了答案(现在第3个表已内部加入PREF_TABLE
,并且第一个加入中添加了其他条件:AND PREF_TABLE.Pref_WebsiteID = LOCATIONS_TABLE.LocationWebsiteID
以防止加载所有位置) :
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'