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 8222377
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T14:15:33+00:00 2026-06-07T14:15:33+00:00

Consider the following tables: users messages —————– ———————– user_id messages msg_id user_id content —————–

  • 0

Consider the following tables:

users                messages
-----------------    -----------------------
user_id  messages    msg_id user_id content
-----------------    -----------------------
1        0           1      1       foo
2        0           2      1       bar
3        0           3      1       foobar
                     4      3       baz
                     5      3       bar

I want to count the number of messages per user and insert the outcome into users.messages, like this:

users                
-----------------    
user_id  messages    
-----------------    
1        3           
2        0           
3        2           

I could use PHP to perform this operation, pseudo:

foreach ($user_id in users) {
  $count = select count(msg_id) from messages where user_id = $user_id
  update users set messages = $count
}

But this is probably very inefficient as compared to one query executed in MySQL directly:

UPDATE users SET messages = (
  SELECT COUNT(msg_id) FROM messages
)

But I’m sure this is not a proper query. Therefore, any help would be appreciated 🙂

  • 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-06-07T14:15:35+00:00Added an answer on June 7, 2026 at 2:15 pm

    Here’s the answer:

    UPDATE `users` AS u 
    SET u.messages=(SELECT COUNT(*) FROM messages WHERE user_id=u.user_id)
    

    and the test:

    http://sqlfiddle.com/#!2/2768b/1

    I just hope these are not huge tables or this could take a while…

    UPDATE:
    Removed unnecessary JOIN from the query
    Thanks to @Ariel and @Zane Bien for correcting my answer!

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

Sidebar

Related Questions

Consider the following tables: users and tweets user_id name tweet_id user_id tweet spam -----------------
Consider following tables structure: users (user_id, ...) images (image_id, user_id, ...) tag_links (tag_id, image_id)
Consider the following tables users tweets --------------------------------- --------------------------- user_id num_retweets sum_retweets tweet_id user_id retweeted
Consider the following database tables: Table messages with 13,000,000 rows (one row per message).
consider the following: 3 tables: Users, Businesses and Addresses. The rows of both Users
Consider the following table: +---------+---------+------------+----------+------------+ | msg_id | user_id | _date | _time |
Consider the following MySQL tables: APPLICATIONS (contains all applications by all users) unique_id |
Consider the following users table: uid followers following ff_fraction -------------------------------------- 1 40 2 20
Consider the following table views user_id _date cnt ------------------------ 1 2011-02-10 123 1 2011-02-11
Consider the following tweets table tweet_id user_id text ----------------------------------------- 1 1 look at 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.