2013-09-23 50 views
1

我敢肯定,這可以用加入,但我小於無知當談到加入來完成:SQL查詢中使用ON或加入

說我有3個表。表中的一個是分支,表2是縣和表3是狀態

表之一:

branchid - Unique record id 
siteCode - Unique identifier for the branch 
address1 - Street address #1 
address2 - Street address #2 
cityName - City Name 
county_ID - record ID from county table 
state_ID - record ID from state table 

表二:

countyid - Unique record ID 
countyName - County Name 

表3

stateid - Unique record ID 
stateShortName - State name two letter abbreviation 
stateLongName - Full state name 

使用正常查詢分支機構,我得到一個記錄,其中包含州和縣表中關聯記錄的編號。

我想這樣做,返回的信息從記錄在表中的一個,並用正確的縣名稱,州名稱

這是否有意義替換記錄ID查詢?

例子:

普通查詢返回:

branchid - 1 
siteCode - CA001 
address1 - 123 Main Street 
address2 - Suite #201 
cityName - San Diego 
county_ID - 234 
state_ID - 2 

正確的查詢:

branchid - 1 
siteCode - CA001 
address1 - 123 Main Street 
address2 - Suite #201 
cityName - San Diego 
county_ID - Sand Diego County 
state_ID - CA 

回答

2

檢查了這一點,這是要執行什麼呢?

SELECT B.branchid, 
B.siteCode, 
B.address1 , 
B.address2 , 
B.cityName , 
C.countyName , 
S.stateShortName 
From branches B 
INNER JOIN counties c ON B.county_ID = C.county_ID 
INNER JOIN states s ON B.state_ID = S.state_ID