SQL Server——将同一表中的一列的值更新到另一列

时间:2022-09-24 19:00:35

I'm trying to overwrite values that are found in TYPE1 with values that are found in TYPE2.

我试图用TYPE2中的值覆盖TYPE1中的值。

I wrote a fiddle to try it out, but for some reason it isn't updating.

我写了一个小提琴来尝试它,但是出于某种原因它没有更新。

http://www.sqlfiddle.com/#!3/a4733/17

3 / a4733/17 http://www.sqlfiddle.com/ !

Any reason why my values in TYPE1 are not updating?

为什么类型1中的值没有更新?

6 个解决方案

#1


29  

This works for me

这适合我

select * from stuff

update stuff
set TYPE1 = TYPE2
where TYPE1 is null;

update stuff
set TYPE1 = TYPE2
where TYPE1 ='Blank';

select * from stuff

#2


16  

UPDATE a
SET a.column1 = b.column2
FROM myTable a 
INNER JOIN myTable b
on a.myID = b.myID

in order for both "a" and "b" to work, both aliases must be defined

为了让“a”和“b”同时工作,必须定义两个别名

#3


7  

UPDATE TABLE_NAME SET COLUMN_A = COLUMN_B;

Much easier. At least on Oracle SQL, i don't know if this works on other dialects as well.

容易得多。至少在Oracle SQL中,我不知道这是否也适用于其他方言。

#4


2  

You put select query before update queries, so you just see initial data. Put select * from stuff; to the end of list.

在更新查询之前放入select查询,因此只看到初始数据。从素材中选择*;到最后。

#5


1  

Your select statement was before the update statement see Updated fiddle

您的select语句是在update语句看到updates之前

#6


0  

This answer about updating column from a part of another column in the same table.

关于从同一表中另一列的一部分更新列的回答。

update T1
set domainname = (New value) --Example: (SELECT LEFT(TableName.col, CHARINDEX('@',TableName.col)-1) STRIPPED_STRING FROM TableName where TableName.col = T2.Emp_ID)
from TableName T1
INNER JOIN
    TableName T2
ON 
    T1.ID= T2.ID;

#1


29  

This works for me

这适合我

select * from stuff

update stuff
set TYPE1 = TYPE2
where TYPE1 is null;

update stuff
set TYPE1 = TYPE2
where TYPE1 ='Blank';

select * from stuff

#2


16  

UPDATE a
SET a.column1 = b.column2
FROM myTable a 
INNER JOIN myTable b
on a.myID = b.myID

in order for both "a" and "b" to work, both aliases must be defined

为了让“a”和“b”同时工作,必须定义两个别名

#3


7  

UPDATE TABLE_NAME SET COLUMN_A = COLUMN_B;

Much easier. At least on Oracle SQL, i don't know if this works on other dialects as well.

容易得多。至少在Oracle SQL中,我不知道这是否也适用于其他方言。

#4


2  

You put select query before update queries, so you just see initial data. Put select * from stuff; to the end of list.

在更新查询之前放入select查询,因此只看到初始数据。从素材中选择*;到最后。

#5


1  

Your select statement was before the update statement see Updated fiddle

您的select语句是在update语句看到updates之前

#6


0  

This answer about updating column from a part of another column in the same table.

关于从同一表中另一列的一部分更新列的回答。

update T1
set domainname = (New value) --Example: (SELECT LEFT(TableName.col, CHARINDEX('@',TableName.col)-1) STRIPPED_STRING FROM TableName where TableName.col = T2.Emp_ID)
from TableName T1
INNER JOIN
    TableName T2
ON 
    T1.ID= T2.ID;