简体   繁体   English

SQL Server - 从同一个表中的数据更新列

[英]SQL Server - Update column from data in the same table

I have a table that looks something like this:我有一张看起来像这样的表:

SetId      ID       Premium
2012        5          Y
2012        6          Y
2013        5          N
2013        6          N

I want to update the 2013 records with the premium values where the setid equals 2012.我想用 setid 等于 2012 的溢价值更新 2013 年的记录。

So after the query it would look like this:所以在查询之后它看起来像这样:

SetId      ID       Premium
2012        5          Y
2012        6          Y
2013        5          Y
2013        6          Y

Any help greatly appreciated非常感谢任何帮助

It's not clear which 2012 value you want to use to update which 2013 value, i've assumed that the ID should be the same.不清楚您想使用哪个 2012 值来更新哪个 2013 值,我假设ID应该相同。

Full example using table variables that you can test yourself in management studio.使用表变量的完整示例,您可以在管理工作室中自行测试。

DECLARE @Tbl TABLE (
    SetId INT,
    Id INT, 
    Premium VARCHAR(1)
)

INSERT INTO @Tbl VALUES (2012, 5, 'Y')
INSERT INTO @Tbl VALUES (2012, 6, 'Y')
INSERT INTO @Tbl VALUES (2013, 5, 'N')
INSERT INTO @Tbl VALUES (2013, 6, 'N')

--Before Update
SELECT * FROM @Tbl 

--Something like this is what you need
UPDATE t 
SET t.Premium = t2.Premium 
FROM @Tbl t 
INNER JOIN @Tbl t2 ON t.Id = t2.Id 
WHERE t2.SetId = 2012 AND t.SetId = 2013

--After Update    
SELECT * FROM @Tbl 
UPDATE t 
SET t.Premium = (SELECT TOP 1 t2.Premium
                 FROM dbo.TableName t2
                 WHERE t2.SetId = 2012)
FROM dbo.TableName t
WHERE t.SetId = 2013 

Demonstration示范

I think this is correct solution:我认为这是正确的解决方案:

UPDATE t 
SET t.Premium = (SELECT TOP 1 t2.Premium
                 FROM dbo.TableName t2
                 WHERE t2.SetId = 2012 AND t2.Id = t.ID)
FROM dbo.TableName t
WHERE t.SetId = 2013 

We can update table from self table, like this:我们可以从 self 表更新表,如下所示:

update TABLE_A 
   set TABLE_A.Col1=B.Col2
  from TABLE_A B 

There is ABSOLUTELY no need to go to all those fancy lengths suggested by the accpeted answer using INNER JOIN s or FROM s or aliases when updating from the same table - see the fiddle here !从同一个表更新时,绝对没有必要使用INNER JOINFROMaliases来达到已接受的答案所建议的所有那些花哨的长度 - 请参阅此处的小提琴!

CREATE TABLE b (x INT, y INT);

Populate:填充:

INSERT INTO b (x) VALUES (2), (3), (4);

Check:检查:

SELECT * FROM test;

Result:结果:

x   y
2   null
3   null
4   null

Now - just simply:现在 - 只是简单地:

UPDATE b SET y = x
WHERE x <= 3;

Then:然后:

SELECT * FROM b;

Result:结果:

x   y
2   2
3   3
4   null

Et voilà - perfectly simple!等等 - 非常简单! Antoine de Saint-Exupéry was right : Antoine de Saint-Exupéry 是对的

“La perfection est atteinte, non pas lorsqu'il n'y a plus rien à ajouter, mais lorsqu'il n'y a plus rien à retirer.” “La perfect est atteinte,non pas lorsqu'il n'y a plus rien à ajouter,mais lorsqu'il n'y a plus rien à retirer。”


Perfection is achieved, not when there is nothing more to add, but when there is nothing left to take away.达到完美,不是当没有什么可以添加的时候,而是当没有什么可以带走的时候。

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

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