2016-10-30 37 views
0

我正在使用以下select語句在我的網站上填充已輸入到3個表中的列表。在加入3個表中消除重複項

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

我剛剛實施的結表作爲任何房源可以有超過1類,但我的問題棱時,我有(ListingX正在使用CategoryY & CategoryZ)會出現在我列出兩次Ca議& 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不在這裏工作,因爲它是第一次從我的聯結表拉動也許查詢需要組織不同?

+0

因爲distinct會應用於查詢的每個輸出列。 –

+0

@CharlesBretana所以這意味着我應用不同的我的JOINS的呢? – UserSN

+0

不,請參閱我編輯的答案。區別應用於整個查詢的最終輸出。如果考慮到整個輸出(所有列),有任何相同的行(所有列值相同),則會消除多餘的重複項。您的查詢選擇查詢中所有表中的所有內容,因此,由於每個類別的類別值不同,因此這些行是不同的並且不會被刪除。您需要將輸出限制爲僅來自Listing表的列。你可以明確地列出你想要的列,或者使用子查詢。 –

回答

1

因爲distinct會應用於查詢的每個輸出列。
什麼表City,Region, CountryCompany in?如果他們在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',請嘗試以下操作:

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 
+0

謝謝你這樣做它可以工作,但是我的類別名稱沒有引入。我的BND_ListingCategories表中沒有顯示一個名爲「CategoryName」的字段。 – UserSN

+1

然後你需要添加類別名稱到輸出 –

+0

我會用UNION嗎? – UserSN