Get previous value after update - MySql

前端 未结 3 1851
悲哀的现实
悲哀的现实 2021-01-27 23:07

I have a table which includes id and name

---    -----  
id     name
---    -----
1      pete

My scenarios looks like this

$sql         


        
相关标签:
3条回答
  • 2021-01-27 23:42

    You cannot fetch the previous name unless you save it to your table as a backup.

    If you want to achieve this create one more column in your table and save the current name before updating to the new one.

    0 讨论(0)
  • 2021-01-27 23:56

    If you are updating one row and you want the previous name, you can use variables:

    set @prevname = '';
    
    update table_name
        set name = if(@prevname := name, 'Alan', 'Alan')
        where id = 1;
    
    select @prevname;
    

    However, I suspect that you really want a slowly changing dimension, and update is not the right operation.

    0 讨论(0)
  • 2021-01-27 23:59

    You can do that by using the OLD and NEW keywords on a trigger on your table.

    Although, i see that you tagged your post as php and I guess you want to get the old value in php, and to do this, you have to do a SELECT before the UPDATE to get the old name, and another SELECT after the UPDATE to get the new name.

    0 讨论(0)
提交回复
热议问题