gpt4 book ai didi

php - 使用 ON 或 JOIN 的 SQL 查询

转载 作者:行者123 更新时间:2023-11-29 06:45:22 25 4
gpt4 key购买 nike

我确信这可以通过连接来完成,但在连接方面我没那么笨:

假设我有 3 张 table 。表一是分支,表二是县,表三是州

表一:

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

表三

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

在分支机构上使用常规查询,我得到了一条记录,其中包含州和县表中关联记录的编号。

我想做一个查询,从表 1 中的记录返回信息,并将记录 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

最佳答案

检查一下,这是您要执行的操作吗?

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

关于php - 使用 ON 或 JOIN 的 SQL 查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18951348/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com