消除连接3个表中的重复项

时间:2016-10-30 16:15:09

标签: sql-server tsql join outer-join

我正在使用以下select语句在我的网站上填充已经输入3个表的列表的网格。

BND_Listing = Main Data
BND_ListingCategories = My Category data for the Listing Table
BND_LingingJunction = Junction Table for multi-category listings.

我刚刚实现了Junction表,因为任何列表都可以有超过1个类别,但是当我有(使用CategoryY& CategoryZ的ListingX)时,我的问题会出现在我的列表中,两次出现在CatX& CatY,因为我的联络表上有这个特定列表的2个条目,所以我理解为什么它会出现两次,但我希望所有内容只显示一次而不是每个类别一次。

SELECT DISTINCT * FROM BND_ListingJunction 
    JOIN BND_listing on BND_listing.LID = BND_ListingJunction.Junc_LID
    JOIN BND_ListingCategories 
       on BND_ListingCategories.CatID = 
            BND_ListingJunction.Junc_CatID
WHERE (CategoryName = '[querystring:filter-Category]' or
          '[querystring:filter-Category]'='All')
    and (City = '[querystring:filter-City]' or 
          '[querystring:filter-City]'='All')
    and (Region= '[querystring:filter-State]' or 
          '[querystring:filter-State]'='All')
    and (Country= '[querystring:filter-Country]' or 
          '[querystring:filter-Country]'='All')
    and isnull(Company,'') <> ''
ORDER by Company ASC

我意识到使用distinct在这里不起作用,因为它是第一次从我的联结表中提取或许查询需要以不同的方式组织?

1 个答案:

答案 0 :(得分:1)

因为distinct应用于查询的每个输出列 CityRegionCountryCompany位于哪个表格中?如果它们在ListingJunction' or ListingCategories`中, 如果您需要明确的列表和类别列表,

SELECT DISTINCT l.*, c.CategoryName 
FROM BND_Listing l
   Join BND_ListingJunction j
      on j.Junc_LID = l.LID
   join BND_ListingCategories c
      on c.CatId = j.Junc_CatID
Where (c.CategoryName = '[querystring:filter-Category]' or
                    '[querystring:filter-Category]'='All')
      and (City = '[querystring:filter-City]' or 
                    '[querystring:filter-City]'='All')
      and (Region= '[querystring:filter-State]' or 
                    '[querystring:filter-State]'='All')
      and (Country= '[querystring:filter-Country]' or 
                    '[querystring:filter-Country]'='All')
      and isnull(Company,'') <> ''
Order by l.Company, l.LID 

如果您想要的是不同的列表清单,请尝试以下方法:

SELECT DISTINCT * FROM BND_Listing l 
Where Exists
  (Select * from BND_ListingJunction j
      join BND_ListingCategories c
          on c.CatId = j.Junc_CatID
   Where j.Junc_LID = l.LID
      and (c.CategoryName = '[querystring:filter-Category]' or
                    '[querystring:filter-Category]'='All')
      and (City = '[querystring:filter-City]' or 
                    '[querystring:filter-City]'='All')
      and (Region= '[querystring:filter-State]' or 
                    '[querystring:filter-State]'='All')
      and (Country= '[querystring:filter-Country]' or 
                    '[querystring:filter-Country]'='All')
      and isnull(Company,'') <> '')
ORDER by l.Company ASC

如果他们在'BND_Listing&#39;中,请尝试:

SELECT DISTINCT * FROM BND_Listing l 
Where Exists
      (Select * from BND_ListingJunction j
         join BND_ListingCategories c
             on c.CatId = j.Junc_CatID
       Where j.Junc_LID = l.LID
           and (c.CategoryName = '[querystring:filter-Category]' or
                    '[querystring:filter-Category]'='All'))
   and (l.City = '[querystring:filter-City]' or 
                    '[querystring:filter-City]'='All')
   and (l.Region= '[querystring:filter-State]' or 
                    '[querystring:filter-State]'='All')
   and (l.Country= '[querystring:filter-Country]' or 
                    '[querystring:filter-Country]'='All')
   and isnull(l.Company,'') <> ''
ORDER by l.Company ASC