2013-10-03 208 views
2

聲明我正在做一個查詢,從表中的行獲取數據,並把它同列下:SQL WHERE SELECT語句中

SELECT [t].*, 
     [d].[Description] AS [Name], 
FROM [TrainerClass] AS [t] join [DescriptionTranslation] AS [d] on [t].[Code] = [d].[Code] 
WHERE [d].[TableName] = 'TrainerClass' AND 
     [d].[FieldName] = 'Name' AND 
     [d].[Language] = 'en-EN'; 

這只是正常,但它返回一列。我想用更多的列獲得相同的結果,但我無法弄清楚如何使用WHERE語句來完成。我想實現的東西,如:

SELECT [t].*, 
     [d].[Description] AS [Name] WHERE [d].[FieldName] = 'Name', 
     [d].[Description] AS [Info] WHERE [d].[FieldName] = 'Info', 
FROM [TrainerClass] AS [t] join [DescriptionTranslation] AS [d] on [t].[Code] = [d].[Code] 
WHERE [d].[TableName] = 'TrainerClass' AND 
     [d].[Language] = 'en-EN'; 

我知道這是不是一個正確的語法,這就是爲什麼我問你如何得到這樣的結果。謝謝提前:)

編輯:

我不想要什麼:

No

我想要什麼:

Yes

(僅做示例一條記錄,應該有更多)

回答

1

如果我正在閱讀您的請求,則需要使用不同的過濾器加入表格兩次。

select t.*, dName.Description as [Name], dInfo.Description as [Info] 
from TrainerClass t 
join DescriptionTranslation dName 
    on t.Code = dName.Code and dName.FieldName = 'Name' 
join DescriptionTranslation dInfo 
    on t.Code = dInfo.Code and dInfo.FieldName = 'Info' 
where 
    dName.TableName = 'TrainerClass' and dName.Language = 'en-EN' 
    and dInfo.TableName = 'TrainerClass' and dInfo.Language = 'en-EN' 
+0

它看起來我的權利,但遺憾的是它不返回任何數據。沒有語法錯誤,但... –

+0

嘗試執行'left join'而不是'join'(默認情況下爲內連接)。看看這是否給你提示你爲什麼沒有得到一個匹配。 –

+0

我得到了同樣的結果... –

4

你可以使用像下面這樣的CASE聲明(我認爲這就是你要找的)。請注意,在每種情況下,不匹配的行都會得到空值,或者您可以將else部分添加到case語句。

SELECT [t].*, 
     CASE WHEN [d].[FieldName] = 'Name' THEN [d].[Description] END AS [Name], 
     CASE WHEN [d].[FieldName] = 'Info' THEN [d].[Description] END AS [Info] 

FROM [TrainerClass] AS [t] join [DescriptionTranslation] AS [d] 
          on [t].[Code] = [d].[Code] 
WHERE [d].[TableName] = 'TrainerClass' AND --[d].[FieldName] = 'Name' AND 
     [d].[Language] = 'en-EN'; 

UPDATE:

根據您的更新,以獲得預期從顯示圖像表的結果,你可以使用Max()功能與Group by

;With cte as 
(
    --your first query goes here 
) 
Select Id,Code, Max(Name) Name, Max(info) Info 
from cte 
Group by Id, Code 

,你可以不CTE做爲:

SELECT t.Id, t.Code, 
     Max(CASE WHEN d.FieldName = 'Name' THEN d.Description END) AS Name, 
     Max(CASE WHEN d.FieldName = 'Info' THEN d.Description END) AS Info 
FROM [TrainerClass] AS t join [DescriptionTranslation] AS d on t.Code = d.Code 
WHERE d.TableName = 'TrainerClass' AND --[d].[FieldName] = 'Name' AND 
     d.Language = 'en-EN' 
GROUP BY t.Id, t.Code; 
+0

我希望具有相同代碼的列的值在同一行上。有沒有簡單的方法來做到這一點? –

+0

不確定你的意思。如果你提供小提琴並用預期結果更新你的問題,這將很容易理解和解決。 – Kaf

+0

在這裏,你可以看看我以前的答案:http://stackoverflow.com/questions/19163001/merging-two-or-more-columns-from-different-sql-queries 希望它有幫助。我還用預期的結果更新了問題。 –

1

您可以使用子查詢進行查詢的另一種方式,它與您的意圖非常相似。但是這種方法只有一個警告,那就是如果你有大的記錄集,它可能會很慢。


SELECT [t].*, 
(SELECT [d].[Description] FROM [DescriptionTranslation] [d] WHERE [d].[FieldName] = 'Name' and [t].[Code] = [d].[Code] [d].[TableName] = 'TrainerClass' AND 
[d].[Language] = 'en-EN') AS [Name], 
(SELECT [d].[Description] FROM [DescriptionTranslation] [d] WHERE [d].[FieldName] = 'Info' and [t].[Code] = [d].[Code] [d].[TableName] = 'TrainerClass' AND 
[d].[Language] = 'en-EN') AS [Info]  
FROM [TrainerClass] AS [t] 
0

使用下面CROSS APPLY您所查詢的

SELECT [t].*, 
     [DTNameDescription].[Description] AS [Name] 
     [DTInfoDescription].[Description] AS [Info] 
FROM [TrainerClass] AS [t] 
     CROSS APPLY (
      SELECT [Description] 
      FROM [DescriptionTranslation] AS [d] 
      WHERE [t].[Code] = [d].[Code] 
         AND [d].[FieldName] = 'Name' 
     ) AS DTNameDescription 
     CROSS APPLY (
      SELECT [Description] 
      FROM [DescriptionTranslation] AS [d] 
      WHERE [t].[Code] = [d].[Code] 
         AND [d].[FieldName] = 'Info' 
     ) AS DTInfoDescription 
WHERE [d].[TableName] = 'TrainerClass' AND 
     [d].[Language] = 'en-EN';