SQL Return 列值作为枚举 - 来自另一列的 GetValue() 字段

SQL Return Column value as Enum - GetValue() field from another Column

这是两个表;

类别

----------------------------------------
Cathegory (tinyint)  |  Name (nvarchar)
----------------------------------------
0                    |  Field
1                    |  Mountain
2                    |  River
----------------------------------------

地点

------------------------------------------
Name (nvarchar)      |  Cathegory(tinyint)
------------------------------------------
Abc                  |  2
Xyz                  |  1
------------------------------------------

当我想检索 Places listing Names and their Cathegories not in the int format but according to the description in Cathegories.

所以检索 Abc 我想要这样;

"River" 而不是 '2'

您需要在 cathegory 上加入两个 table,如下所示。

select
    p.name as places,
    c.name as category_name
from places p
join cathegories c
on p.cathegory = c.cathegory

请使用以下查询,

select c.name as place, p.name as name from Cathegories c
inner join Places p
on (c.Cathegory  = p.Cathegory);

这是基于已接受答案的多个 JOIN 语句的版本:

SELECT 

ColumnUserViews,

C.Cathegory AS VCathegory, 

ColumnUserPoints, 

T.Description AS VTag1,
TT.Description AS VTag2


FROM dbo.Users U

JOIN dbo.Cathegories C ON U.Cathegory = C.Cathegory
JOIN dbo.Tags T ON U.Tag1 = T.Tag
JOIN dbo.Tags TT ON U.Tag2 = TT.Tag

关键字U定义物理table用户,T标签,TT也定义标签(可自由重命名)。 VCathegory 是一个新的要检索的虚拟列,它保存 Users.Cathegory 的值,根据当前方案转换为 Cathegories.Description(等效字符串)。