简体   繁体   中英

Display column name of most recently updated column

I am wondering if it is possible to display the column name of the most recently updated column in a table.

Example:

**Original Record created on 03/20/14:**
Name: John
Height
Weight
Age
Update_date: 03/20/14
Update_Column: Name

Then someone comes in and updates the height on 03/22/14:

Name: John
Height: 5'9 
Weight
Age
Update_date: 03/22/14
Update_Column: Height

And the Update_date and Update_column would change again if someone came in and put a value for age. And so on.

Is this possible?

Also, if the above is possible, would it be possible to display the column name farthest right if a user updated more than one column at the same time?

Example:

User updates the below record:
Name: John
Height: 5'9 
Weight
Age
Update_date: 03/22/14
Update_Column: Height

And adds in a weight and age at the 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 would display Age because it is the farthest to the right. (Thinking of it as you read from left to right, so the column that was last updated would be the one farthest right)

So to sum things up, I need to be able to display Updated_date(which will be the current date when the record is last updated) and Updated_Col (which is the column name of the last column that was updated, and if multiple columns are updated then display the one that a value was updated last in)

Hopefully the examples help to clarify things.

Thanks, Steven

You need to store those meta data for each row. So you'd need two new columns, the update_date and update_column. Then you can add an before update trigger to check which columns are about to change and set the update date.

update Here's an example:

delimiter //
create table a (
  id int (10) unsigned auto_increment,
  a int(10),
  b int(10),
  update_date datetime NULL,
  update_column varchar(16) NULL,
  primary key (id)
)//

create trigger bu_a before update on a for each row begin
  set NEW.update_date = NOW();
  set NEW.update_column = NULL;
  -- you need to start with the rightmost column if you want
  -- that matched with the highest priority
  if OLD.b != NEW.b then
    set NEW.update_column = "b";
  elseif OLD.a != NEW.a then
    set NEW.update_column = "a";
  end if;
end //

Test:

insert into a (id,a,b) values (1,1,1), (2,1,1), (3,1,1), (4,1,1)[
update a set b = 2 where id = 2;
update a set a = 2 where id = 3;
update a set a = 2 where id = 4;
update a set b = 2 where id = 4;
select * from a;

Output:

ID  A 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 a
4   2 2 March, 24 2014 23:22:33+0000 b

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM