2017-08-16 72 views
1

林不知道如何解釋這一點。最好的我可以解釋它是,我想做一個類似於左連接的東西,只有我想返回連接表的主表匹配條件的子集,但仍然返回所有左連接的行。我希望這是有道理的。過濾器加入,但仍然返回所有行

我有4個表:

公司

服務

ServicesTranslations

CompanyService

,公司可以有許多服務和服務可以有很多翻譯。 我想在ServiceTranslation表上篩選給定的搜索詞。

我能得到這個工作的唯一方法是在SQL下面,但我覺得也許有更好的更乾淨的方法來做到這一點,或者我的解決方案好嗎?

 DECLARE @defaultLanguage NVARCHAR(5) 

     SET @defaultLanguage = (SELECT LanguageCode FROM LANGUAGE WHERE IsDefault = 1) 

     SELECT 
      c.Id, 
      c.Name, 
      c.OrgNr, 
      c.UserId, 
      c.Address, 
      c.PostalArea, 
      c.County, 
      c.Country, 
      c.Description, 
      c.DescriptionFull, 
      c.Telephone, 
      c.Email, 
      c.Website, 
      c.IsApproved, 
      s.Id, 
      ISNULL(tr.Name, def.Name) Name 

      FROM Company c 

      INNER JOIN CompanyService cs on cs.CompanyId = C.Id 

      INNER JOIN Service s on s.Id = cs.ServiceId 

      LEFT OUTER JOIN ServiceTranslation tr 
      ON s.Id = tr.ServiceId AND tr.LanguageCode = @userLanguage 
      -- join default language of the service: 

      LEFT OUTER JOIN ServiceTranslation def 
      ON s.Id = def.ServiceId AND def.LanguageCode = @defaultLanguage 

      WHERE c.IsApproved = 1 

      AND 
      c.Id IN 
      (
       SELECT c1.Id FROM Company c1 

       INNER JOIN CompanyService cs1 on cs1.CompanyId = c1.Id 

       INNER JOIN Service s1 on s1.Id = cs1.ServiceId 

       INNER JOIN ServiceTranslation tr1 
       ON s1.Id = tr1.ServiceId AND tr1.LanguageCode = @userLanguage 
       -- join default language of the service: 

       INNER JOIN ServiceTranslation def1 
       ON s1.Id = def1.ServiceId AND def1.LanguageCode = @defaultLanguage 



       AND s1.IsApproved = 1 AND ISNULL(tr1.Name, def1.Name) LIKE '%' + @searchQuery + '%' 
     ) 

在此先感謝!

編輯:

我想一些澄清是必需的。我不知道如何以可讀格式添加表格數據,所以我附上了表格和所需結果的一些圖像。

Company表:

enter image description here

服務表:

enter image description here

ServiceTranslation表:

enter image description here

CompanyService表:

enter image description here

期望的結果(@userLanguage = 'NB-NO' 和@searchquery = '交通'):

它有與所述字服務兩家公司的運輸'我想要這兩家公司加上所有其他服務。

enter image description here

你可以從結果看圖像,IM過濾器公司有一個匹配的服務,但還是想回到那些符合公司和所有服務。

我希望這使得它更有點清楚:)

+3

我認爲樣本數據和預期的結果將真正幫助。你想要返回哪個子集? –

+0

您在主SELECT語句中的s.Id後缺少一個逗號,它包含c.Website,c.IsApproved,s.Id,ISNULL(tr.Name,def.Name)Name' –

+0

@ J.D。對不起,這是一個錯字,我現在修好了,謝謝。 – JCoder23

回答

0

使用CTE:

;WITH Comps AS 
(
    SELECT DISTINCT c.CompanyId FROM CompanyService C 
    LEFT JOIN ServiceTranslation tr ON tr.ServiceId = C.ServiceId AND tr.LanguageCode = @userLanguage AND tr.Name LIKE '%' + @searchQuery + '%' 
    LEFT JOIN ServiceTranslation def ON def.ServiceId = C.ServiceId AND def.LanguageCode = @defaultLanguage AND def.Name LIKE '%' + @searchQuery + '%' 
    WHERE tr.ServiceId IS NOT NULL OR def.ServiceId IS NOT NULL 
) 

    SELECT 
     c.Id, 
     c.Name, 
     c.OrgNr, 
     c.UserId, 
     c.Address, 
     c.PostalArea, 
     c.County, 
     c.Country, 
     c.Description, 
     c.DescriptionFull, 
     c.Telephone, 
     c.Email, 
     c.Website, 
     c.IsApproved, 
     s.Id, 
     def.Name 

     FROM Company c 

     INNER JOIN CompanyService cs on cs.CompanyId = C.Id 

     INNER JOIN Service s on s.Id = cs.ServiceId 

     INNER JOIN ServiceTranslation def ON def.ServiceId = s.Id AND def.LanguageCode = @defaultLanguage 

     INNER JOIN Comps CM ON CM.CompanyId = c.Id 

     WHERE c.IsApproved = 1 AND s.IsApproved = 1 

或使用子查詢:

SELECT 
      c.Id, 
      c.Name, 
      c.OrgNr, 
      c.UserId, 
      c.Address, 
      c.PostalArea, 
      c.County, 
      c.Country, 
      c.Description, 
      c.DescriptionFull, 
      c.Telephone, 
      c.Email, 
      c.Website, 
      c.IsApproved, 
      s.Id, 
      def.Name 

      FROM Company c 

      INNER JOIN CompanyService cs on cs.CompanyId = C.Id 

      INNER JOIN Service s on s.Id = cs.ServiceId 

      INNER JOIN ServiceTranslation def ON def.ServiceId = s.Id AND def.LanguageCode = @defaultLanguage 

      INNER JOIN Comps CM ON CM.CompanyId = c.Id 

      WHERE c.IsApproved = 1 AND s.IsApproved = 1 AND c.Id IN 
      (
      SELECT DISTINCT c.CompanyId FROM CompanyService C 
      LEFT JOIN ServiceTranslation tr ON tr.ServiceId = C.ServiceId AND tr.LanguageCode = @userLanguage AND tr.Name LIKE '%' + @searchQuery + '%' 
      LEFT JOIN ServiceTranslation def ON def.ServiceId = C.ServiceId AND def.LanguageCode = @defaultLanguage AND def.Name LIKE '%' + @searchQuery + '%' 
      WHERE tr.ServiceId IS NOT NULL OR def.ServiceId IS NOT NULL 
     ) 
+0

我已經嘗試過,但只返回匹配搜索參數的公司和服務行。我已經添加了一些更詳細的信息來解釋後面的內容。謝謝 – JCoder23

+0

感謝您的澄清。更新我提出的解決方案... –

+0

非常感謝,我知道必須有一種更乾淨的方式來做到這一點!這非常有見地! – JCoder23

相關問題