Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3313496
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T22:07:20+00:00 2026-05-17T22:07:20+00:00

I have a table of comments and a table of posts Whenever a post

  • 0

I have a table of comments and a table of posts

Whenever a post is deleted, a query runs to subtract the number of comments (that are deleted later) from each user’s comment_count

So if a user has 2 comments in a post, and that post is deleted, their balance should have 2 subtracted from it

My query is as follows:

    UPDATE users 
INNER JOIN comment ON users.id = comment.author 
       SET comment_count = comment_count - 1 
     WHERE comment.post = 1

User A has 2 comments with .post = 1, but for some reason that user only gets comment_count subtracted by 1 once, when it should happen twice

I think my syntax is right because when I:

    SELECT * 
      FROM users 
INNER JOIN comment ON users.id = comment.author 
     WHERE comment.post = 1

I get two results for user A

Shouldn’t UPDATE be iterating over those two results, subtracting each time?

Can someone explain what I am missing? thank you

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T22:07:20+00:00Added an answer on May 17, 2026 at 10:07 pm

    If you’re going to store the count, use:

    UPDATE USERS
       SET comment_count = (SELECT COUNT(*)
                              FROM COMMENT c
                             WHERE c.author = USERS.id)
    

    …or:

    UPDATE USERS u
      JOIN (SELECT c.author,
                   COUNT(*) AS numComments
              FROM COMMENT c
          GROUP BY c.author) x ON x.author = u.id
       SET comment_count = x.numComments
    

    There’s no point in relying on two records to subtract twice, when you could perform the operation once.

    I prefer not to store such values, because they can be calculated based on records without the hassle of keeping the counts in sync. A view might be a better idea…

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a a Comments table and a Posts table, where a Post has
I have posts , votes , and comments tables. Each post can have N
The following query selects all posts and each post's owner, all of the comments
I have two tables Posts and comments Post Table Post_id Post_content Comments table comment_id
I have two tables as posts and comments. Each row in comments table has
I have one table that saves comments for a varied set of content types.
Ok I have a table in my SQL Server database that stores comments. My
I have two tables posts and comments . Table comments have post_id attribute. I
I have two tables. One table contains comments on posts, another contains commenter information
Let's say I have a Posts table and a Comments table. I want my

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.