当前位置: 代码迷 >> Sql Server >> 用表中的一个字段更新另一个字段解决思路
  详细解决方案

用表中的一个字段更新另一个字段解决思路

热度:48   发布时间:2016-04-24 10:41:49.0
用表中的一个字段更新另一个字段
表格式为
id     username   create
1        zh                   2
2        zs                    2
3       ts                    1
4      gg                   3
我想把create中的值换成对应的username
id     username   create
1        zh                   zs
2        zs                   zs
3       ts                    zh
4      gg                   ts

这是结果
------解决方案--------------------

select a.id,a.username,b.username as create from table as a inner join table as b on a.id=b.create

update a set username =b.username  from table as a inner join table as b on a.id=b.create



------解决方案--------------------

update a set a.create  =b.username 
from mytable a , mytable b 
where a.create = b.id
  相关解决方案