I have a table which includes id and name
--- -----
id name
--- -----
1 pete
My scenarios looks like this
$sql
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.
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.
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.