您可以使用case
,但实际上我喜欢派生表的想法,因为将来更容易更改:
select
R.*, C.Name as RequestStatusName
from Request as R
left outer join (values
(1, 'IN_PROGRESS'),
(2, 'CANCELED'),
(3, 'COMPLETED')
) as C(Id, Name) on C.Id = R.RequestStatusID
因此,您可以添加任意数量的列,例如:
select
R.*,
C.Name as RequestStatusName,
C.Description as RequestStatusDescription
from Request as R
left outer join (values
(1, 'IN_PROGRESS', 'In progress description'),
(2, 'CANCELED', 'Canceled description'),
(3, 'COMPLETED', 'Completed description')
) as C(Id, Name, Description) on C.Id = R.RequestStatusID
如果您必须经常加入,您可以创建视图:
create view vw_RequestStatusNames
as
select C.Id, C.Name, C.Description
from (values
(1, 'IN_PROGRESS', 'In progress description'),
(2, 'CANCELED', 'Canceled description'),
(3, 'COMPLETED', 'Completed description')
) as C(Id, Name, Description)
并在连接中使用它:
select
R.*,
C.Name as RequestStatusName,
C.Description as RequestStatusDescription
from Request as R
left outer join vw_RequestStatusNames as C on C.Id = R.RequestStatusID
=> SQL FIDDLE 演示