Select and update same table and row in one connection in mysql -


this may seem repeat question, seems questions involving more 1 table. when row in table selected, need +1 view count in same row. know can't use trigger or 2 statements in same query, both of these things done single connection database? preferred method both select row , +1 view field?

it can done in same connection, can't think of way done using 1 query.

here how in single transaction;

start transaction; update tbl set view=view+1 id = 10;  select * tbl id = 10; commit; 

a second "better" method can eliminates having read tbl table twice.

update tbl  set view = (@viewscount := view + 1) id = 10; 

and new value of views count this

select @viewscount; 

a third way utilizing last_insert_id() function so

update tbl  set view = last_insert_id(view) + 1 id = 10; 

then new view count need execute query after update. can not execute other queries after update otherwise not intended value.

   select last_insert_id(); 

Comments

Popular posts from this blog

c# - Validate object ID from GET to POST -

node.js - Custom Model Validator SailsJS -

php - Find a regex to take part of Email -