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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T22:42:44+00:00 2026-06-02T22:42:44+00:00

i have USERS TABLE that contains the following fields _id name number_of_posts and i

  • 0

i have USERS TABLE that contains the following fields _id name number_of_posts

and i have POSTS TABLE that contains the following fields _id user_id post_text

no the relationship between the users and posts is one- to -many i.e one user have many post

the question is how to update the number_of_posts in users table such that it will hold the number of post in the posts table

UPDATE

I have these tables filled with about hundreds of records there is no way to use the trigger to update the number_of_posts .

  • 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-02T22:42:45+00:00Added an answer on June 2, 2026 at 10:42 pm

    You can do this with a trigger on the posts table. Every time a post is inserted, update the number_of_posts on the users table.

    See http://dev.mysql.com/doc/refman/5.6/en/triggers.html

    Update: full worked solution

    drop table if exists users;
    create table users (  
       _id bigint unsigned auto_increment primary key,   
       name varchar(50) not null, 
       number_of_posts integer not null default 0
    );
    
    drop table if exists posts;
    create table posts (  
       _id bigint unsigned auto_increment primary key,   
       user_id bigint unsigned not null, 
       post_text text
    );
    
    -- Populate with dummy data
    insert into users (name) values ('Bob'), ('Sally');
    insert into posts (user_id, post_text) 
        values (1, 'First!!'), (1, 'Second...'), 
               (2, 'Post 1'), (2, 'another post'), (2, 'more posts');
    
    -- One-time update of users table
    update users u
    set u.number_of_posts = (      
      select count(0) from posts p
      where u._id = p.user_id      
    );
    
    -- trigger to keep post count up to date when future posts are made
    drop trigger if exists trg_post_count;
    delimiter $$
    
    create trigger trg_post_count
    after insert on posts
    for each row 
    begin
        select count(0) into @post_count 
        from posts
        where user_id = NEW.user_id;
    
        update users
        set number_of_posts = @post_count
        where _id = NEW.user_id;
    end;
    $$
    
    delimiter ;
    
    -- test trigger
    insert into posts (user_id) values (2);
    select * from users;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a table that contains articles posted by users. The structure of the
I have a table that contains intervals: CREATE TABLE tbl ( user_id: INTEGER, start:
I have a table that contains id, created, user_id. I'm wondering if there is
I have a user table 'users' that has fields like: id first_name last_name ...
In my database table I have the following fields: Table Supplier: id name vehicles
following on from my other post... I have a table that uses two fields
I have a database structure that has a Person table which contains fields such
i have one table, which contains that fields mid , email 1 , 'user_a@...'
I want to have a form that contains a table of fields and some
I have a table (user) that contains user information. I have another table (userview)

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.