简体   繁体   English

使用ON或JOIN的SQL查询

[英]SQL query using ON or JOIN

I'm sure this can be done with a join but I'm less that clueless when it comes to joins: 我敢肯定,这可以通过联接来完成,但是对于联接,我却毫不迟疑:

Say that I have 3 tables. 假设我有3张桌子。 Table one is branches, table 2 is counties and table 3 is states 表一是分支机构,表二是县,表三是州

Table one: 表一:

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

Table two: 表二:

countyid - Unique record ID
countyName - County Name

Table three 表三

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

Using a normal query on branches, I get a record with the number of the associated record in the state and county table. 使用对分支的常规查询,我得到了一条记录,其中包含州和县表中的相关记录编号。

I'd like to do a query that returns to info from a record in table one and replaces the record id with the correct county name and state name 我想执行一个查询,该查询从表1中的记录返回信息,并用正确的县名和州名替换记录ID。

Does that make sense? 那有意义吗?

Example: 例:

Normal query return: 正常查询返回:

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

Correct query: 正确的查询:

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

check this out, is this what you want to perform? 检查一下,这是您要执行的操作吗?

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 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM