retrieve last updated column in mysql

北慕城南 提交于 2019-12-10 12:05:52

问题


I have a MySQL query that goes as follows (using Zend_Db):

 $sql = $handle->quoteInto("UPDATE board SET rank=rank+1 WHERE post_id=?", $postid);
 $handle->query($sql);

(Rank isn't an auto-incrementing PK). I would like to now retrieve the value of rank without preforming another query. I've tried $handle->lastInsertId(); but it doesn't seem to work , since I didn't use MySQL's natural auto-incrementing method (I can't - rank is the rank of a post. I either ++ or -- it.)

Any way to do this with preforming another query? A function that will return the last changed value?


回答1:


I don't believe this is possible - you'll just have to do a SELECT.




回答2:


You can use the LAST_INSERT_ID function that MySQL provides to set a value and then make it available via the mysql_insert_id() C function that the $handle->lastInsertId(); relies on.

The following is an updated version of your code snippet with the LAST_INSERT_ID changes made to it:

 $sql = $handle->quoteInto("UPDATE board SET rank=LAST_INSERT_ID(rank+1) WHERE post_id=?", $postid);
 $handle->query($sql);

Let me know if you have any questions.

HTH,

-Dipin



来源:https://stackoverflow.com/questions/1087698/retrieve-last-updated-column-in-mysql

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!