Get previous value after update - MySql

前端 未结 3 1889
悲哀的现实
悲哀的现实 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: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.

提交回复
热议问题