简体   繁体   中英

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.

So retrieving Abc I want it like this;

"River" instead the '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.

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:

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). 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM