使用SQL Server 2008 R2查询时,如何用另一列替换获取的值 [英] How to replace fetched values with another column while querying with SQL Server 2008 R2

查看:170
本文介绍了使用SQL Server 2008 R2查询时,如何用另一列替换获取的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,让我用示例来解释我的问题

Alright let me explain my question with example

我们有1张桌子 该表包含

We have 1 table This table contains

Id
Name
Number

现在的例子

1 House 4
2 Hospital 3
3 Airport 1
4 Station 2

现在当以select * from table

我想用代表姓名的数字替换第三列数字值

I want to replace third column number values with that number representing Name

因此获取示例

1 House Station
2 Hospital Airport
3 Airport House
4 Station Hospital

我该怎么做?谢谢

推荐答案

select t1.id, 
       t1.name,
       t2.name as name2
from your_table t1
left join your_table t2 on t1.number = t2.id

您可以两次连接同一张表,以将number替换为name.联接中的on竞争条件再次与表匹配,然后您可以从该表中选择name(t2)

You can join the same table twice to replace the number with the name. The on contidion in the join matches the table again and then you can select the name from that table (t2)

这篇关于使用SQL Server 2008 R2查询时,如何用另一列替换获取的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆