MySQL 更新列 +1?

发布于 2024-09-25 21:17:44 字数 67 浏览 5 评论 0原文

我想知道将列更新为 +1 最简单的方法是什么?我将根据用户提交新帖子的时间来更新类别的帖子计数。

谢谢。

I was wondering what would be the easiest way to update a column by +1? I will be updating a post count of a category based on when users submits a new post.

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

人海汹涌 2024-10-02 21:17:44

最简单的方法是存储计数,而是依靠 COUNT 聚合函数来反映数据库中的值:

   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

您可以创建一个视图来容纳上述查询,这样您就可以像查询表一样查询视图...

但如果您决定存储号码,请使用:

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

..替换“?”具有适当的值。

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.

岁吢 2024-10-02 21:17:44

您可以执行以下操作:

UPDATE categories SET posts = posts + 1 WHERE category_id = 42;代码>

You can do:

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

逆光飞翔i 2024-10-02 21:17:44

怎么样:

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

How about:

update table
set columnname = columnname + 1
where id = <some id>
无语# 2024-10-02 21:17:44
update post set count = count + 1 where id = 101
update post set count = count + 1 where id = 101
诠释孤独 2024-10-02 21:17:44
update table_name set field1 = field1 + 1;
update table_name set field1 = field1 + 1;
神仙妹妹 2024-10-02 21:17:44

如何更新订单列计数值

尝试

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

How to update order column Count value

Try

  UPDATE order SET                                      
    Order_Count = Order_Count + 100
  WHERE 
    Order_ID = '1234'
甜扑 2024-10-02 21:17:44
update TABLENAME
set COLUMNNAME = COLUMNNAME + 1
where id = 'YOURID'
update TABLENAME
set COLUMNNAME = COLUMNNAME + 1
where id = 'YOURID'
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文