开发者

Know the just incremented value

We everybody know how to in开发者_如何转开发crement a column in MySQL:

update table set col = col + 1 where id = 15;

But ... is there anyway to know the new value WITHOUT doing a SELECT query ?

Thank you


Trying to understand your question. Taken from Posted by Justin Swanhart on July 29 2004 5:32pm@Mysql UPDATE refman. Maybe this is what you trying to achieve for

update table set 
col = col + 1 
where id = 15
and @value := col

The @value := col will always evaluate to true and will store the col value before the update in the @value variable.

You could then do

select @value;


Not that I'm aware of.

If you think about it, since multiple rows could be affected by an UPDATE, it's not trivial nor necessarily attractive to think about the database returning all of that data in the context of an UPDATE.

The MySQL docs also address the "can I do UPDATE and SELECT in the same subquery" question:

Currently, you cannot update a table and select from the same table in a subquery.

http://dev.mysql.com/doc/refman/5.5/en/update.html

One alternative that may work for you is to rely on the SELECT..FOR UPDATE approach:

http://dev.mysql.com/doc/refman/5.5/en/innodb-locking-reads.html

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜