将第一列值更新为第二列 [英] update 1st column value to 2nd column

查看:104
本文介绍了将第一列值更新为第二列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我的桌子上有数据类似的东西 -



id1 id2

1 5

2 8

3 6



输出如 - < br $>


id1 id2

5 1

8 2

6 3


我想将第二列中第一列和第二列中的所有值交换为第一列。

Hi All,

I have a table with data like something as -

id1 id2
1 5
2 8
3 6

Output like -

id1 id2
5 1
8 2
6 3

I want to swap value all values of 1st column in 2nd column and 2 columns value into 1st column.

推荐答案

SQL Server:交换列值的简单方法 [ ^ ]



交换-的值 - 的两列s-in-SQL-Server.html [ ^ ]



查看链接..希望它会有所帮助...
SQL Server: Simple way to swap column values[^]

Swap-the-values-of-two-columns-in-SQL-Server.html[^]

Check the links..hope it will help...






试试这个



Hi,

try this

create table test
(
col1 int,
col2 int
)

insert into test values(1,4),(10,5),(2,10),(5,9)





简单一个





simple one

select * from test

UPDATE test SET col1=col2, col2=col1

select * from test







现在复杂的一个;)




Now the complex one ;)

select * from test

update t1
set t1.col1=t2.c1,t1.col2=t2.c2
from test t1
join
(
select col2 as c1,col1 as c2 from test
)t2
on t1.col1=t2.c2

select * from test


如果你只想显示val在第2列代替column1,反之亦然,那么可以简单地获取数据



选择id2为'id1',id1为'id2'从表
if you just want to display the value at column2 in place of column1 and visa versa then can simply fetch data as

select id2 as 'id1',id1 as 'id2' from table


这篇关于将第一列值更新为第二列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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