使用 where 子句更新 SQL 中的记录时处理重复项

Handling duplicates while updating records in SQL using where clause

我有一种情况,我需要更新 table 中的一行,当遇到重复条目时,然后根据另一个列值做出决定。

例如,假设我的 table 是这样的:salary_table

 Salary    Username   Usersurname  Last_entry_date

  3000      abc         bak             20-feb-13

  4000      sdf         kup             20-mar-15

  5000      abc         bak             20-mar-15

所以我的更新查询是这样的

update salary_table 
set salary=9000 
where username=abc 
  and usersurname=bak;

对于像第 2 行这样的记录,当有唯一条目时,这不会造成任何问题

但我会得到多行记录,例如 1(1 和 3),但我只想更新一行。在这种情况下,我想检查 last_entry_date。并且具有最新日期的条目(在本例中为第 3 行)应该得到更新。

如何做到?

非常感谢任何帮助。

在这种情况下,您必须添加 "where clause" "last_entry_date = ??" 如果不添加适当的过滤器,您将如何确定要更新的行。

Update salary_table
set salary = 9000
where username= 'abc'
  and usersurname= 'bak'
  and Last_entry_date = (select max(Last_entry_date)
                        from SalaryTable
                        where s.username = username
                        and s.usersurname = usersurname);