检测要选择的列,具体取决于同一个表的列值

时间:2015-04-02 10:55:52

标签: sql database sql-server-2008

我正在尝试构建一个查询,该查询根据同一个表的列返回表的不同列。比如说,一列代表一种项目。

这是我的临时解决方案,我只是将多个查询的结果组合在一起。

如您所见,我使用" 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 个答案:

答案 0 :(得分: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)