使用 SQL Server 2008 R2 查询时如何用另一列替换获取的值 [英] How to replace fetched values with another column while querying with SQL Server 2008 R2
本文介绍了使用 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屋!
查看全文