[英]SQL Return Column value as Enum - GetValue() field from another Column
Here are the two tables;这是两张表;
Cathegories分类
----------------------------------------
Cathegory (tinyint) | Name (nvarchar)
----------------------------------------
0 | Field
1 | Mountain
2 | River
----------------------------------------
Places地方
------------------------------------------
Name (nvarchar) | Cathegory(tinyint)
------------------------------------------
Abc | 2
Xyz | 1
------------------------------------------
When I want to retrieve the Places listing Names and their Cathegories not in the int format but according to the description in Cathegories.当我想检索列出名称及其分类的地方时,不是 int 格式,而是根据分类中的描述。
So retrieving Abc I want it like this;所以检索 Abc 我想要这样;
"River" instead the '2' “河”而不是“2”
Please use below query,请使用以下查询,
select c.name as place, p.name as name from Cathegories c
inner join Places p
on (c.Cathegory = p.Cathegory);
You need to join two table on cathegory
as shown below.您需要在分类上加入两个表,
cathegory
所示。
select
p.name as places,
c.name as category_name
from places p
join cathegories c
on p.cathegory = c.cathegory
Here is a version of multiple JOIN statements based on the accepted answer:这是基于接受的答案的多个 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
The keyword U defines physical table Users, T Tags, and TT also Tags (free to rename).关键字U定义了物理表Users、T Tags,TT也是Tags(可自由重命名)。 VCathegory is a new virtual Column to be retrieved which holds the value of Users.Cathegory translated into Cathegories.Description ('s string equivalent) as per this current scheme.
VCathegory 是一个要检索的新虚拟列,它保存根据当前方案转换为 Cathegories.Description (的字符串等效项)的 Users.Cathegory 的值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.