使用 COUNT 个其他字段更新列是 SQL?

发布于 2024-11-10 03:02:45 字数 378 浏览 7 评论 0原文

我设置了以下表格:

Articles:
ID | TITLE | CONTENT | USER | NUM_COMMENTS

COMMENTS
ID | ARTICLE_ID | TEXT

我需要一个 sql 语句,该语句使用针对文章的评论计数来更新文章表的 NUM_Comments 字段,例如:

update articles a, comments f 
set a.num_comments =  COUNT(f.`id`)
where f.article_id = a.id

上面的 sql 不起作用,我得到一个 Invalid Use fo Group function错误。我在这里使用MySQL。

I have the following tables set up:

Articles:
ID | TITLE | CONTENT | USER | NUM_COMMENTS

COMMENTS
ID | ARTICLE_ID | TEXT

I need a sql statement which updates the NUM_Comments field of the articles table with teh count of the comments made against the article like:

update articles a, comments f 
set a.num_comments =  COUNT(f.`id`)
where f.article_id = a.id

The sql above doesn't work and I get an Invalid Use fo Group function error. I'm using MySQL Here.

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

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

发布评论

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

评论(5

久夏青 2024-11-17 03:02:45

您不能在更新语句中加入联接。应该是这样,

update articles
set num_comments =
(select count (*) from comments
where comments.article_id = articles.id)

这将更新整个文章表,这可能不是您想要的。如果您只想更新一篇文章,请在子查询后添加“where”子句。

You can't have a join in an update statement. It should be

update articles
set num_comments =
(select count (*) from comments
where comments.article_id = articles.id)

This will update the entire articles table, which may not be what you want. If you intend to update only one article then add a 'where' clause after the subquery.

北城挽邺 2024-11-17 03:02:45

这应该有效。

UPDATE articles a SET num_comments = 
(SELECT COUNT(*) FROM comments c WHERE c.article_id = a.id)

但我宁愿在发表评论时只更新一条记录:

UPDATE articles a SET num_comments = 
(SELECT COUNT(*) FROM comments c WHERE c.article_id = 100) WHERE a.id = 100

This should work.

UPDATE articles a SET num_comments = 
(SELECT COUNT(*) FROM comments c WHERE c.article_id = a.id)

But i would rather update only one record when comment has been posted:

UPDATE articles a SET num_comments = 
(SELECT COUNT(*) FROM comments c WHERE c.article_id = 100) WHERE a.id = 100
极致的悲 2024-11-17 03:02:45

count (*) 可能有一些问题,特别是 count 和 (*) 之间有空格...

因此在 sqlite、pgsql 上运行 sql 将是:

update articles 
  set num_comments = 
    (select count(id) from comments 
     where comments.article_id = articles.id)

count (*) might have some problems, especially with blank space between count and (*) ...

so working sql on sqlite, pgsql would be:

update articles 
  set num_comments = 
    (select count(id) from comments 
     where comments.article_id = articles.id)
上课铃就是安魂曲 2024-11-17 03:02:45

要仅根据列计数进行更新,您可以执行以下操作:

update articles,
 (select count (*) 
  from comments
  where comments.article_id = articles.id) as newtotals
set articles.num_comments = newtotals.count;

或者...如果您遇到需要滚动计数的情况:

update articles,
 (select (count (*)) + (articles.num_comments) as count 
  from comments
  join articles on 
    comments.article_id = articles.id
  group by articles.id) as newtotals
set articles.num_comments = newtotals.count;

To update based on a column count alone, you could do something like:

update articles,
 (select count (*) 
  from comments
  where comments.article_id = articles.id) as newtotals
set articles.num_comments = newtotals.count;

or ... if you had a situation that required rolling counts:

update articles,
 (select (count (*)) + (articles.num_comments) as count 
  from comments
  join articles on 
    comments.article_id = articles.id
  group by articles.id) as newtotals
set articles.num_comments = newtotals.count;
风和你 2024-11-17 03:02:45

你不能用通用的内连接方式来做到这一点。但您可以通过另一种方式执行此操作:

1- 从文章表中选择所有 id

2- 迭代它们并执行以下命令

update posts set NUM_COMMENTS = (select count(id) from comments where id = $id) where id = $id

进一步增强它,在第一个选择中不要选择所有值,特别是当该表太大时,您需要迭代文章并每次迭代获取 1000 条记录。这样您就可以从数据库池中维护健康的数据库线程,并且还可以节省带宽。

you cant do it in a generic inner join way. but you can do it in another way by:

1- Select all the ids from the articles table

2- iterate them and execute the following command

update articles set NUM_COMMENTS = (select count(id) from comments where id = $id) where id = $id

to enhance it more, in the 1st select dont select all the values especially when that table is too large, you need to iterate the articles and get 1000 records per iteration. This way u will maintain a healthy DB threads from your DB pool and you also save bandwidth.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文