0

I have a situation where i need to update a row in table and when faced with a duplicate entry then take a decision based on another column value.

For example let's say my table is like this : 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

so my update query is something like this

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

For records like row 2 when there is unique entry this will not cause any problem

but i will get multiple rows for records like 1 (1 and 3) but i only want to update one row. In this case i would like to check last_entry_date. And the entry which has latest date (row 3 in this case) should get updated.

How can it be done ?

Any help would be highly appreciated.

2 Answers 2

1
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);
Sign up to request clarification or add additional context in comments.

Comments

0

you have to add "where clause" on what you want in this case "last_entry_date = ??" With out adding proper filter how you identify which row to be updated.

1 Comment

hi , i mean how you update if 3 or more rows are matching ... you should have correct application logic to update which row on what criteria.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.