如果不为null,则从同一字段更新空字段

时间:2018-02-28 07:35:38

标签: mysql sql-update mariadb

我想更新所有NULL PlayerId字段,将现有的PlayerId与完全相同的Name匹配到同一个表中

我在同一个名字中有一些NULL PlayerID和一些具有良好价值的东西:

    SELECT count(*) as c, Name, PlayerId FROM my_table 
    WHERE Name = 'John,Doh' GROUP BY PlayerId order by c ASC;


+---+-----------+-------------+
| c | Name      | PlayerId    |
+---+-----------+-------------+
| 2 | John,Doh  |        NULL |
| 8 | John,Doh  |     2900084 |
+---+-----------+-------------+

我不知道它是否可以来自同一张桌子,还是应该创建一个临时的?

谢谢,

1 个答案:

答案 0 :(得分:1)

update your_table t1
join your_table t2 on t1.name = t2.name
                  and t2.playerId is not null
set t1.playerId = t2.playerId
where t1.playerId is null