将数据从同一表中的一行复制到另一行. [英] Copying data from one row to another within the same table.

查看:355
本文介绍了将数据从同一表中的一行复制到另一行.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个奇怪的要求(以前从未见过),我需要用一个id更新一行以在同一张表中更新具有其他id的行,让我解释一下我有一个类似
的表

HI i had a strange requirement (never seen before) where i need to update a row with one id to update a row with other id in same table let me explain i have a table like

ID   Name   Comm    Fees
1    abc    23.5     1.2
1    xyz    56.5     2.4
2    abc     2.3      62.95
2    xyz     4.3     11.89



所以我在这里需要做的是用2nd ID Fees行更新1st ID Comm行,是否有可能在同一张表中执行更新值,输出看起来应该像



so what i need to do here is i have to update the 1st ID Comm row with 2nd ID Fees row , is it possible to do a update values from the same table the output should look like

ID   Name   Comm    Fees
1    abc    62.95     1.2
1    xyz    11.89     2.4
2    abc     2.3      62.95
2    xyz     4.3     11.89




在此先感谢




Thanks in advance

推荐答案

如果连接列为Name,则类似于:
If the connecting column is Name then something like:
UPDATE MyTable
SET Comm = (SELECT Fees 
            FROM MyTable mt2
            WHERE mt2.Name = MyTable.Name
            AND mt2.Id != MyTable.Id)


但是,如果您有两个以上的同名行,则失败,因此应扩展这种情况下的条件.


However that fails if you have more than 2 rows with the same name so the condition in that case should be expanded.


这篇关于将数据从同一表中的一行复制到另一行.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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