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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T15:05:07+00:00 2026-06-17T15:05:07+00:00

I have four tables that contain some fields 1. user(id, name, email, password, …..)

  • 0

I have four tables that contain some fields

1. user(id, name, email, password, .....)
2. policy1(id, userid, ....)
3. policy2(id, userid, ....)
4. policy3(id, userid, ....)

Now this is clear that user’s primary key (id) is foreign key in other three tables.

I want to fetch total number of tables against each user that contain user id in that table.

for example:

user id 1 has entry in any two tables, 
user id 2 has entry in three tables, 
user id 3 has entry in any single table

Result should be like

id    total
1     2
2     3
3     1

I tried using Left Join but that I could not get the expected result.

  • 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-17T15:05:08+00:00Added an answer on June 17, 2026 at 3:05 pm

    You can use a subquery to get the count for each user and then add the values together to get the final result. I am using a LEFT JOIN to return all user rows even if there is not a matching value in the other tables. If you only want the users with values in the other tables, then you can use an INNER JOIN:

    select u.id,
      Coalesce(CntP1, 0) + Coalesce(CntP2, 0) + Coalesce(CntP3, 0) TotalCount
    from `user` u
    left join
    (
      select count(*) CntP1, userid
      from policy1
      group by userid
    ) p1
      on u.id = p1.userid
    left join
    (
      select count(*) CntP2, userid
      from policy2
      group by userid
    ) p2
      on u.id = p2.userid
    left join
    (
      select count(*) CntP3, userid
      from policy3
      group by userid
    ) p3
      on u.id = p3.userid
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have four tables user-question contains two columns: questionID, userID, the questions that the
I have four different tables, one main SPECIAL table containing only id's that reference
I have a LINQ to SQL DataContext that queries four different tables. But I
I have a database that has 16 tables, but only four are relevant to
I have four tables: Users PrivilegeGroups rdPrivileges LinkPrivilege ----------- ---------------- --------------- --------------- userId(pk) privilegeGroupId(pk)
I have four tables in my database and i want to join that's and
I have an sql query with inner joins of four tables that takes more
In my project i have request page that contain a datalist with four templates
Let's assume that I have two tables... Foo and Bar. They contain the following
I have four tables, USER, CONTACT, CONACT_TYPE, and USER_CONTACT USER_CONTACT stores all of the

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.