【问题标题】:Sql Update duplicateSql 更新重复
【发布时间】:2017-05-27 16:12:46
【问题描述】:

我有一个 serial_no(主键),我想替换 serial_no 中的一个单词。例如:

Update table Set 
serial_no=REPLACE(serial_no,'56','000')

但收到错误重复更新

我想更新所有记录,除了记录重复

【问题讨论】:

标签: sql database replace duplicates


【解决方案1】:

如果你使用 MySQL,你可以使用UPDATE IGNORE:

UPDATE IGNORE table
SET serial_no = REPLACE(serial_no,'56','000')

【讨论】:

    【解决方案2】:

    使用NOT EXISTS 确保新的serial_no 不存在。

    Update table t1
    Set serial_no=REPLACE(serial_no,'56','000')
    where not exists (select 1 from table t2
                      where t2.serial_no = REPLACE(t1.serial_no,'56','000'))
    

    【讨论】:

      猜你喜欢
      • 2013-06-15
      • 2013-02-09
      • 2013-11-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-05-10
      • 2016-05-30
      相关资源
      最近更新 更多