2015-04-02 104 views
0

我想構建一個查詢,該查詢根據同一個表的列返回表的不同列。假設一列代表一種物品。根據同一個表的列值檢測要選擇的列

這是我的臨時解決方案,我只是結合多個查詢的結果。如您所見,我使用「AS」關鍵字來重命名默認列名稱。

SELECT [custom].[dbo].[records].[id] AS "ID",[type_name] AS "Тип записи", [domain_name] AS "Значение", [custom].[dbo].[records].[record_type_id] FROM [custom].[dbo].[records] INNER JOIN [custom].[dbo].[record_type] ON [custom].[dbo].[records].record_type_id = [custom].[dbo].[record_type].id WHERE [custom].[dbo].[records].record_type_id = 1 
union all 
SELECT [custom].[dbo].[records].[id] AS "ID",[type_name] AS "Тип записи", CAST([ip_address] AS nvarchar(15)) AS "Значение", [custom].[dbo].[records].[record_type_id] FROM [custom].[dbo].[records] INNER JOIN [custom].[dbo].[record_type] ON [custom].[dbo].[records].record_type_id = [custom].[dbo].[record_type].id WHERE [custom].[dbo].[records].record_type_id = 2 
union all 
SELECT [custom].[dbo].[records].[id] AS "ID",[type_name] AS "Тип записи", [domain_name] AS "Значение", [custom].[dbo].[records].[record_type_id] FROM [custom].[dbo].[records] INNER JOIN [custom].[dbo].[record_type] ON [custom].[dbo].[records].record_type_id = [custom].[dbo].[record_type].id WHERE [custom].[dbo].[records].record_type_id = 3 
union all 
SELECT [custom].[dbo].[records].[id] AS "ID",[type_name] AS "Тип записи", [mail_domain] AS "Значение", [custom].[dbo].[records].[record_type_id] FROM [custom].[dbo].[records] INNER JOIN [custom].[dbo].[record_type] ON [custom].[dbo].[records].record_type_id = [custom].[dbo].[record_type].id WHERE [custom].[dbo].[records].record_type_id = 4 
union all 
SELECT [custom].[dbo].[records].[id] AS "ID",[type_name] AS "Тип записи", CAST([ip_address] AS nvarchar(15)) AS "Значение", [custom].[dbo].[records].[record_type_id] FROM [custom].[dbo].[records] INNER JOIN [custom].[dbo].[record_type] ON [custom].[dbo].[records].record_type_id = [custom].[dbo].[record_type].id WHERE [custom].[dbo].[records].record_type_id = 5 
union all 
SELECT [custom].[dbo].[records].[id] AS "ID",[type_name] AS "Тип записи", [domain_name] AS "Значение", [custom].[dbo].[records].[record_type_id] FROM [custom].[dbo].[records] INNER JOIN [custom].[dbo].[record_type] ON [custom].[dbo].[records].record_type_id = [custom].[dbo].[record_type].id WHERE [custom].[dbo].[records].record_type_id = 6 
union all 
SELECT [custom].[dbo].[records].[id] AS "ID",[type_name] AS "Тип записи", [service_name] AS "Значение", [custom].[dbo].[records].[record_type_id] FROM [custom].[dbo].[records] INNER JOIN [custom].[dbo].[record_type] ON [custom].[dbo].[records].record_type_id = [custom].[dbo].[record_type].id WHERE [custom].[dbo].[records].record_type_id = 7 
+1

它的列,而不是場。 – jarlh 2015-04-02 11:05:14

回答

1

與CASE操作查詢會更短

SELECT 
[custom].[dbo].[records].[id] AS "ID", 
[type_name] AS "Тип записи", 

case [custom].[dbo].[records].record_type_id 
when 1 then [domain_name] 
when 2 then ... 
when 7 then [service_name] 
end AS "Значение", 

[custom].[dbo].[records].[record_type_id] 
FROM [custom].[dbo].[records] 
INNER JOIN [custom].[dbo].[record_type] 
ON [custom].[dbo].[records].record_type_id = [custom].[dbo].[record_type].id 
WHERE [custom].[dbo].[records].record_type_id in (1, 2... 7) 
+0

Братишь+1всёработаетбодренько! Благодарностьотдушиотсердца! – 2015-04-02 11:17:08

相關問題