How to replace fetched values with another column while querying with SQL Server 2008 R2

为君一笑 提交于 2019-12-01 21:47:56
select t1.id, 
       t1.name,
       t2.name as name2
from your_table t1
left join your_table t2 on t1.number = t2.id

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)

SQLFiddle Example

You can do this with an explicit join:

select t.id, t.name, t2.name as otherName
from t left outer join
     t t2
     on t2.number = t.id
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!