基于其他列值更新列值的SQL脚本

基于其他列值更新列值的SQL脚本,sql,sql-server,database,scripting,Sql,Sql Server,Database,Scripting,我刚开始编写SQL脚本,现在正在使用SQL Server 我有一个数据表: 正如您所看到的,一些race\u title\u短值为空。我想根据race_identifier_代码更新此列中的值 例如,其中一个种族识别码是CORL,它的种族头衔比珊瑚杯短 其中一项记录的种族被确定为CORL,但没有种族名称 如何根据包含比赛标题短信息的比赛标识符代码更新空比赛标题短信息?您可以使用相关子查询: update t set race_title_short = (select max(t2.ra

我刚开始编写SQL脚本,现在正在使用SQL Server

我有一个数据表:

正如您所看到的,一些race\u title\u短值为空。我想根据race_identifier_代码更新此列中的值

例如,其中一个种族识别码是CORL,它的种族头衔比珊瑚杯短

其中一项记录的种族被确定为CORL,但没有种族名称


如何根据包含比赛标题短信息的比赛标识符代码更新空比赛标题短信息?

您可以使用相关子查询:

update t
    set race_title_short = (select max(t2.race_title_short)
                            from t t2
                            where t2.race_identifier_code = t.race_identifier_code
                           )
    where race_title_short is null;
使用自联接:

update t
set t.race_title_short = tt.race_title_short
from tablename t inner join tablename tt
on tt.race_identifier_code = t.race_identifier_code
where t.race_title_short is null and tt.race_title_short is not null

更新t1集合t1.race_title_short=从测试t2中选择不同的race_title_short,其中 t2.race\u identifier\u code=t1.race\u identifier\u code
和t2.race_title_short在测试t1中不为空

使用更新语句:updateyourtable SET race_title_short='Coral Cup',其中race_identifier_code='CORL';语法可能会根据您的RDBMS而有所不同。使用该信息更新标记,我们可以更好地指导您。使用您正在使用的数据库标记您的问题。