简体   繁体   English

SQL使用来自同一表中其他行的数据更新行

[英]SQL Updating rows with data from other rows in the same table

I have a large table of records where the primary key is an item code which is an int. 我有一个很大的记录表,其中主键是商品代码,它是一个整数。

Lots of the records are out of date and I want to replace the information in those rows with information in other rows while keeping the old primary key. 许多记录已过时,我想用其他行中的信息替换那些行中的信息,同时保留旧的主键。

So if I had two rows: 因此,如果我有两行:

ITEM_CODE   LANGUAGE    LABEL
12345678    ENG         OLD LABEL
87654321    EN-GB       NEW LABEL

Im looking to write an SQL statement that will leave me with the following table 我正在寻找写一条SQL语句,该表格将留给我下表

ITEM_CODE   LANGUAGE    LABEL
12345678    EN-GB       NEW LABEL
87654321    EN-GB       NEW LABEL

When I looked at the UPDATE statement I was looking for something like 当我查看UPDATE语句时,我正在寻找类似的东西

UPDATE table
SET  language1 and label1 WITH language2 and label 2 
WHERE item code1 = item code 2

Im doing this all in SQL Server Management Studio 我在SQL Server Management Studio中完成了所有这些工作

Hope you can help. 希望能对您有所帮助。 Thanks 谢谢

change your code like below: 更改您的代码,如下所示:

UPDATE table SET  LANGUAGE = (Select LANGUAGE from table where ITEM_CODE = 87654321) 
                  LABEL    = (Select LABEL from table where ITEM_CODE = 87654321)
WHERE ITEM_CODE = 12345678

it will work for your example, also you can rewrite it in other syntax and ways..... 它适用于您的示例,也可以用其他语法和方式重写它.....

if you are a different goal, please explain it more. 如果您的目标不同,请进一步说明。

good luck 祝好运

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM