简体   繁体   English

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

[英]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.

相关问题 从 SQL 中的另一列派生一列的值 - Deriving value of 1 column from another column in SQL 返回一个列值的结果,但不返回另一条SQL的结果 - Return results for one column value but not another SQL SQL-返回与另一列值匹配的一列中的最小值 - SQL - return the smallest value in one column that matches the value of another column 根据oracle中的另一个列字段值返回列名称 - return a column name depending upon another column field value in oracle Oracle-SQL查询以从另一个表中的列返回值,其中值等于另一个列 - Oracle - SQL Query to return a value from a column in another table where value equals a different column SQL 查询返回与具有特定值的另一列关联的列 - SQL query to return column that is associated to another column with a certain value 如何用同一视图表 (SQL) 中另一个字段(不同列)的值替换一个字段中的值? - How to replace a value in one field by a value from another field (different column) within the same view table (SQL)? SQL-从一列返回所有唯一的值对,在另一列中返回每个值 - SQL - Return all unique pairs of values from one column, for each value in another column 需要在一列中查找值,然后从另一列返回值 - Need to find value in one column and then return value from another column 返回依赖于另一列中的值的值 - Return values that relies on a value from another column
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM