How to increment a counter and return the value in MySQL

前端 未结 1 1604
太阳男子
太阳男子 2020-12-28 16:41

I have a table that has a Key column and a Counter column. I need to do something like this:

SELECT counter=counter+1 FROM table WHERE key=\'mykey\'
<         


        
相关标签:
1条回答
  • 2020-12-28 17:30
    update mytable set count=last_insert_id(counter+1) where key='mykey'
    

    Then

    select last_insert_id()
    

    last_insert_id() can be passed an argument to 'set' it (and return that value), and calling it without an argument will return the value again. The internal state used by last_insert_id() is per-connection, which means that the same update statement issued on another connection will not affect the first.

    Reference: last_insert_id()

    0 讨论(0)
提交回复
热议问题