mysql - Auto Increment column when row is updated -


i have mysql table structure seen below. last column represents number of downloads of file. each time file accessed, it's uuid changes.

table structure:

+---------+--------------------+--------------+-------------+ |  dkey   |     file_name      |     file     |  downloads  | +---------+--------------------+--------------+-------------+ |  uuid   |     file.mp4       |  file alias  |      0      | |  uuid2  |  another_file.mov  |  file alias  |      3      | +---------+--------------------+--------------+-------------+ 

i wondering if there way increment download count if row updated. it's not requirement, can change when change uuid, wondering if there way this.

i looked around , couldn't find showed how it, or if couldn't done.

you can use trigger.

the syntax this:

create trigger increment_downloads before update on mytable each row begin   set new.downloads = old.downloads + 1 end; 

edit

when creating trigger, may need change delimiter, this:

delimiter // create trigger increment_downloads before update on mytable each row begin   set new.downloads = old.downloads + 1; end; // delimiter ; 

i tested in mysql first inserting row this:

insert mytable (uuid, downloads) values ('test', 0); 

i updated so:

update mytable set uuid = 'test1' uuid = 'test'; 

and when pulled row, downloads equal 1:

enter image description here


note: in trigger, cannot set value of new. column in after update trigger, why answer edited use before.


Comments

Popular posts from this blog

c++ - Difference between pre and post decrement in recursive function argument -

php - Nothing but 'run(); ' when browsing to my local project, how do I fix this? -

php - How can I echo out this array? -