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

  • Home
  • SEARCH
  • 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 6033051
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T05:25:38+00:00 2026-05-23T05:25:38+00:00

I have a SQL Database with 3 tables: table1: user id | nickname 1

  • 0

I have a SQL Database with 3 tables:

table1: user

id | nickname
1    peter
2    tom

table2: posts

user_id | title | text
1         hmmm    bla
1         yeah    blub
2         blub    dada
1         yuhu    asdf

table3: messages

user_id | title | message
1         hello   bla
2         hi      blub
2         hey     dada
2         huhu    asdf

How do I count every entry from posts/messages for each user in one query?

  • 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-23T05:25:39+00:00Added an answer on May 23, 2026 at 5:25 am

    Try this:

    SELECT a.user_id, 
           COALESCE(b.cnt_b, 0) posts_count, 
           COALESCE(c.cnt_c, 0) messages_count
      FROM user a LEFT JOIN 
                (
                    SELECT user_id, COUNT(1) cnt_b
                        FROM posts b 
                     GROUP BY user_id
                ) b
         ON a.user_id = b.user_id LEFT JOIN
                (
                    SELECT user_id, COUNT(1) cnt_c
                        FROM messages c 
                     GROUP BY user_id
                ) c
        ON a.user_id = c.user_id
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an SQL database with multiple tables, and I am working on creating
I have an SQL database with a set of tables that have Unique Id's.
I have designed database tables (normalised, on an MS SQL server) and created a
I have two tables Team_DATA and Driver_PROFILE_DATA in an SQL database. For every driver_profile
I have a SQL Server 2000 database with around a couple of hundred tables.
I have SQL Server 2008 Ent and OLTP database with two big tables. How
I have a linq to sql database. Very simplified we have 3 tables, Projects
We have two tables in a SQL Server 2005 database, A and B.There is
SQL Server 2008 Database Question. I have 2 tables, for arguments sake called Customers
Anyone have a PL-SQL statement that i can use to generate database & tables

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.