mysql - Display column name of most recently updated column -
i wondering if possible display column name of updated column in table.
example:
**original record created on 03/20/14:** name: john height weight age update_date: 03/20/14 update_column: name
then comes in , updates height on 03/22/14:
name: john height: 5'9 weight age update_date: 03/22/14 update_column: height
and update_date , update_column change again if came in , put value age. , on.
is possible?
also, if above possible, possible display column name farthest right if user updated more 1 column @ same time?
example:
user updates below record: name: john height: 5'9 weight age update_date: 03/22/14 update_column: height
and adds in weight , age @ same time on 03/24/14:
name: john height: 5'9 weight: 150 age: 31 update_date: 03/22/14 update_column: height
the update_column display age because farthest right. (thinking of read left right, column last updated 1 farthest right)
so sum things up, need able display updated_date(which current date when record last updated) , updated_col (which column name of last column updated, , if multiple columns updated display 1 value updated last in)
hopefully examples clarify things.
thanks, steven
you need store meta data each row. you'd need 2 new columns, update_date , update_column. can add before update
trigger check columns change , set update date.
update here's example:
delimiter // create table ( id int (10) unsigned auto_increment, int(10), b int(10), update_date datetime null, update_column varchar(16) null, primary key (id) )// create trigger bu_a before update on each row begin set new.update_date = now(); set new.update_column = null; -- need start rightmost column if want -- matched highest priority if old.b != new.b set new.update_column = "b"; elseif old.a != new.a set new.update_column = "a"; end if; end //
test:
insert (id,a,b) values (1,1,1), (2,1,1), (3,1,1), (4,1,1)[ update set b = 2 id = 2; update set = 2 id = 3; update set = 2 id = 4; update set b = 2 id = 4; select * a;
output:
id b update_date update_column 1 1 1 (null) (null) 2 1 2 march, 24 2014 23:22:33+0000 b 3 2 1 march, 24 2014 23:22:33+0000 4 2 2 march, 24 2014 23:22:33+0000 b
Comments
Post a Comment