【问题标题】:Join Multiple tables (basically the same table) with Joins使用 Joins 连接多个表(基本上是同一个表)
【发布时间】:2013-09-06 20:53:37
【问题描述】:

我有三个表(没有创建数据库)。它们本质上相同,但数据略有不同,它们有 Company_Id 和 Category_Id。我还想将类别名称链接到此表,所以我必须加入类别。 在加入之前这个工作

Select Distinct CompanyId, Category_ID From Product1 where Product1.CompanyId = 10
union
Select Distinct Distinct CompanyId, Category_ID From Product2 where Product2.CompanyId = 10
union
Select Distinct Distinct CompanyId, Category_ID From Product3 where Product3.CompanyId = 10

这为我提供了基于产品 ID 的不同产品和类别列表。

我现在需要将类别名称添加到类别表中。我试过了:

    Select Distinct CompanyId, Category_ID From Product1 where Product1.CompanyId = 10
inner join Category on Category.Id=Product1.Category_ID
    union
    Select Distinct Distinct CompanyId, Category_ID From Product2 where Product2.CompanyId = 10
inner join Category on Category.Id=Product2.Category_ID
    union
    Select Distinct Distinct CompanyId, Category_ID From Product3 where Product3.CompanyId = 10
inner join Category on Category.Id=Product3.Category_ID

但是返回 无法绑定多部分标识符“Company.CATEGORY_ID”。

有什么方法可以只显示所有 3 个表中具有类别名称的不同项目?

(所以如果 product1 和 product2 都有一个 Company_ID = 10 的产品,并且有两个类别,它只会显示两次)

【问题讨论】:

    标签: sql sql-server tsql join


    【解决方案1】:

    where 位于from 子句之后。 join 关键字是from 的一部分,所以它需要放在where 之前:

    Select CompanyId, Category_ID
    From Product1 inner join
         Category
         on Category.Id = Product1.Category_ID
    where Product1.CompanyId = 10
    union
    Select CompanyId, Category_ID From Product2 
    From Product2 inner join
         Category
         on Category.Id = Product1.Category_ID
    where Product2.CompanyId = 10
    union
    Select CompanyId, Category_ID From Product3
    From Product3 inner join
         Category
         on Category.Id = Product3.Category_ID
    where Product1.CompanyId = 10;
    

    另外,distinct distinct 可能会返回错误。您不需要distinct 关键字,因为您使用的是union 而不是union all。

    【讨论】:

      【解决方案2】:
          Select distinct CompanyId, Category_ID From Product1
      inner join Category on Category.Id=Product1.Category_ID where Product1.CompanyId = 10
          union
          Select distinct CompanyId, Category_ID From Product2
      inner join Category on Category.Id=Product2.Category_ID  where Product2.CompanyId = 10
          union
          Select distinct CompanyId, Category_ID From Product3
      inner join Category on Category.Id=Product3.Category_ID where Product3.CompanyId = 10
      

      这没有经过测试,但如果没有错字,它应该可以解决你的问题。

      【讨论】:

        【解决方案3】:

        试试这个。我认为先做union 然后加入Category 表会更易读和维护:

        with cte as (
            select distinct CompanyId, Category_ID from Product1 where Product1.CompanyId = 10
            union 
            select distinct CompanyId, Category_ID from Product2 where Product2.CompanyId = 10
            union
            select distinct CompanyId, Category_ID from Product3 where Product3.CompanyId = 10
        )
        select
           P.CompanyId, P.Category_ID, c.Category_Name
        from cte as P
           inner join Category as c on c.Id = P.Category_ID
        

        或者,如果您不喜欢common table expressions,请使用子查询:

        select
           P.CompanyId, P.Category_ID, c.Category_Name
        from (
            select distinct CompanyId, Category_ID from Product1 where Product1.CompanyId = 10
            union 
            select distinct CompanyId, Category_ID from Product2 where Product2.CompanyId = 10
            union
            select distinct CompanyId, Category_ID from Product3 where Product3.CompanyId = 10
        )as P
           inner join Category as c on c.Id = P.Category_ID
        

        【讨论】:

          猜你喜欢
          • 2011-05-03
          • 1970-01-01
          • 1970-01-01
          • 2013-06-10
          • 2015-05-18
          • 2015-07-24
          • 2018-12-19
          • 1970-01-01
          相关资源
          最近更新 更多