【问题标题】:Joining two different tables in the Join clause [closed]在 Join 子句中连接两个不同的表 [关闭]
【发布时间】:2021-09-28 05:51:52
【问题描述】:

我有三个表表 A、表 B 和表 C。

Table A:AId, id, state, prop_id, category
Table B:BId, id, state, prop_id, MainProductid, price
Table C:CId, id, state, category, mainproductid, subProductid, price
Example Data:
Aid id  prop_id category        
1   1   SH      SCA500      
2   1   PH      SCA500      
3   1   PK      SCA500      
4   1   LP      SCB300      
5   1   LQ      SCB300      
                    
Bid id  prop_id mainProductId   price   
1   1   SH      Base            30  
2   1   SH      BasePlus        40  
3   1   PH      Base            30  
4   1   PH      BasePlus        50  
5   1   LQ      Base            35  
                    
Cid id  category    mainProductId   subProductid    price
1   1   SCA500      Base            Lower Margin    25
2   1   SCB300      Base            Lower Margin    30
3   1   SCA500      BasePlus        Upper Margin    25
4   1   SCB300      BasePlus        Upper Margin    30

declare  @TableA table(Aid int, id int, prop_id varchar(100), category varchar(100))
declare  @TableB table(Bid int, id int, prop_id varchar(100), mainProductId varchar(100), price int) 
declare @TableC table(Cid int, id int, Category varchar(100), mainProductId varchar(100), subProductid varchar(100), price int)

Insert into @tableA values (1,1,'SH','SCA500'), (2,1,'PH','SCA500'),(3,1,'PK','SCA500'),(4,1,'LP','SCB300'),(5,1,'LQ','SCB300')
                     
Insert Into @tableB values (1,1,'SH','Base',30),(2,1,'SH','BasePlus',40),(3,1,'PH','Base',30),(4,1,'PH','BasePlus',50),(5,1,'LQ','Base',35)
                                                                               
Insert into @tableC values (1,1,'SCA500','Base','Lower Margin',25),(2,1,'SCB300','Base','Lower Margin',30),(3,1,'SCA500','BasePlus','Upper Margin',25),(4,1,'SCB300','BasePlus','Upper Margin',30)

预期结果:

id  prop_id category    mainproductid   mainPrice   SubProductId    subPrice
1   SH      SCA500      Base            30          Lower Margin    25
1   SH      SCA500      Baseplus        40          Upper Margin    25
1   PH      SCA500      Base            30          Lower Margin    25
1   PH      SCA500      Baseplus        50          Upper Margin    25
1   LQ      SCA500      Baseplus        35          Lower Margin    30

一个类别可以有多个 prop_id。 我必须得到所有的主要产品、它们的子产品和它们的价格。 下面的查询是我必须做的。

Select a.id,a.prop_id,a.category,b.mainProductId, b.price 'mainPrice',c.subproductid,c.price 'subPrice'
From TableA a
Join TableB b
On a.id = b.id
And a.prop_id = b.prop_id
Join TableC c
On b.id = c.id
And b.mainproductid = c.mainProductid
And a.category = c.category

如果我们在表 C 中有 prop_id,那么我不需要在最后一个子句中对表 A 进行额外的连接条件。这是好的数据库设计吗? 在 join 子句中加入多个表有什么缺点?谢谢!

【问题讨论】:

  • 感谢卢克的回复。我很抱歉我没有详细说明这个问题。为了将表 C 连接到表 B,由于数据库的设计方式,我还必须将其连接到表 A。我会更新问题以便更好地理解。
  • 如果你说,在(内部)连接之后,a.id = b.id 那么每个 a.id 都等于 b.id,对于 a.id 和 c.id 也是如此。可以得出结论,指定b.id=c.id是相当无用的。
  • 很抱歉给您带来了困惑。我已经更新了这个问题。它可能会让你更清楚我在问什么。
  • 一些示例数据和预期结果将有助于澄清您的要求
  • 问:“这是好的数据库设计吗?答:否 问:“在连接子句中加入多个表有什么缺点?” 答:在 SO 上,您应该只一次问 1 个问题,为了回答第二个问题,必须知道表中定义了哪些索引(目前您没有显示任何索引定义。

标签: sql sql-server tsql join


【解决方案1】:

我不确定你是否需要 TableA。

SELECT B.prop_id
, B.mainProductId
, B.price AS mainPrice   
, C.Category
, C.subProductid
, C.price AS subPrice
FROM [dbo].[TableB] B
INNER JOIN [dbo].[TableC] C ON B.mainProductId = C.mainProductId

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-08-18
    • 2017-03-20
    • 1970-01-01
    • 1970-01-01
    • 2019-03-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多