0

我有三个表(没有创建数据库)。它们本质上相同,但数据略有不同,它们有 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 的产品,并且有两个类别,它只会显示两次)

4

3 回答 3

2

在子句where之后。from关键字是 of的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.

于 2013-09-06T21:00:22.757 回答
1
    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

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

于 2013-09-06T21:22:34.857 回答
0

尝试这个。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

或者,如果您不喜欢公用表表达式,请使用子查询:

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
于 2013-09-06T21:05:56.223 回答