开发者

MySQL Update Column +1?

I was wondering what would be the easiest way to update a column by +1? I will be updating a post co开发者_如何学编程unt of a category based on when users submits a new post.

Thanks.


The easiest way is to not store the count, relying on the COUNT aggregate function to reflect the value as it is in the database:

   SELECT c.category_name,
          COUNT(p.post_id) AS num_posts
     FROM CATEGORY c
LEFT JOIN POSTS p ON p.category_id = c.category_id

You can create a view to house the query mentioned above, so you can query the view just like you would a table...

But if you're set on storing the number, use:

UPDATE CATEGORY
   SET count = count + 1
 WHERE category_id = ?

..replacing "?" with the appropriate value.


You can do:

UPDATE categories SET posts = posts + 1 WHERE category_id = 42;


How about:

update table
set columnname = columnname + 1
where id = <some id>


update post set count = count + 1 where id = 101


update table_name set field1 = field1 + 1;


How to update order column Count value

Try

  UPDATE order SET                                      
    Order_Count = Order_Count + 100
  WHERE 
    Order_ID = '1234'


update TABLENAME
set COLUMNNAME = COLUMNNAME + 1
where id = 'YOURID'
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜