消除 Join over 3 Tables 中的重复项

Eliminate duplicates in Join over 3 Tables

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

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

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

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 是行不通的,因为它是第一次从我的连接处拉出 table 也许查询需要以不同的方式组织?

因为 distinct 应用于查询的每个输出列。
table 中的 CityRegionCountryCompany 是什么?如果它们在 ListingJunction' orListingCategories` 中, 如果您需要不同的清单和类别列表,

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”中,试试这个:

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