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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T14:13:36+00:00 2026-05-27T14:13:36+00:00

I have a query that is getting counts from multiple tables by using a

  • 0

I have a query that is getting counts from multiple tables by using a LEFT JOIN and subqueries. The idea is to get a count various activites a member has participated in.

The schema looks like this:

member
PK member_id

table1
PK tbl1_id
FK member_id

table2
PK tbl2_id
FK member_id

table3
PK tbl3_id
FK member_id

My query looks like this:

SELECT t1.num1,t2.num2,t3.num3
FROM member m
LEFT JOIN
(
   SELECT member_id,COUNT(*) as num1
   FROM table1
   GROUP BY member_id
) t1 ON t1.member_id = m.member_id
LEFT JOIN
(
   SELECT member_id,COUNT(*) as num2
   FROM table2
   GROUP BY member_id
) t2 ON t2.member_id = m.member_id
LEFT JOIN
(
   SELECT member_id,COUNT(*) as num3
   FROM table3
   GROUP BY member_id
) t3 ON t3.member_id = m.member_id
WHERE m.member_id = 27

Where 27 is a test id. The actual query joins more than three tables and the query is run multiple times with the member_id being changed. The problem is this query runs pretty slow. I get the info I need but I am wondering if anyone could suggest a way to optimize this. Any advice is very much appreciated. Thanks much.

  • 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-27T14:13:37+00:00Added an answer on May 27, 2026 at 2:13 pm

    You should refactor your query. You can do this by reordering the way the query collects the data. How?

    • Apply the WHERE clause first
    • Apply JOINs last

    Here is your original query:

    SELECT t1.num1,t2.num2,t3.num3 
    FROM member m 
    LEFT JOIN 
    ( 
       SELECT member_id,COUNT(*) as num1 
       FROM table1 
       GROUP BY member_id 
    ) t1 ON t1.member_id = m.member_id 
    LEFT JOIN 
    ( 
       SELECT member_id,COUNT(*) as num2 
       FROM table2 
       GROUP BY member_id 
    ) t2 ON t2.member_id = m.member_id 
    LEFT JOIN 
    ( 
       SELECT member_id,COUNT(*) as num3 
       FROM table3 
       GROUP BY member_id 
    ) t3 ON t3.member_id = m.member_id 
    WHERE m.member_id = 27 
    

    Here is you new query

    SELECT
       IFNULL(t1.num1,0) num1,
       IFNULL(t1.num2,0) num2,
       IFNULL(t1.num3,0) num3
    FROM
    (
       SELECT * FROM member m 
       WHERE member_id = 27
    ) 
    LEFT JOIN 
    ( 
       SELECT member_id,COUNT(*) as num1 
       FROM table1 
       WHERE member_id = 27
       GROUP BY member_id 
    ) t1 ON t1.member_id = m.member_id 
    LEFT JOIN 
    ( 
       SELECT member_id,COUNT(*) as num2 
       FROM table2 
       WHERE member_id = 27
       GROUP BY member_id 
    ) t2 ON t2.member_id = m.member_id 
    LEFT JOIN 
    ( 
       SELECT member_id,COUNT(*) as num3 
       FROM table3 
       WHERE member_id = 27
       GROUP BY member_id 
    ) t3 ON t3.member_id = m.member_id 
    ;
    

    BTW I changed member m into SELECT * FROM member m WHERE member_id = 27 in case you need any information about member 27. I also added the IFNULL function to each result to produce 0 in case count is NULL.

    You need to make absolutely sure

    • member_id is the primary key of the member table
    • member_id is indexed in table1, table2, and table3

    Give it a Try !!!

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

Sidebar

Related Questions

I'm getting odd results from a MySQL SELECT query involving a LEFT JOIN ,
I am using in C# MYsql .I have query that works if I run
I have a query that I'm executing from a .NET application to a SQL
I have a query that is currently using a correlated subquery to return the
I have a query that pulls up questions from one table and answers from
SELECT * from `employees` a LEFT JOIN (SELECT phone1 p1, count(*) c, FROM `employees`
I have the following query that looks for transactions that cancel themselves out from
Trying to get a simple COUNT from a table that takes a couple of
I have a LIST (query output)that has the distinct ID’s from a table Account.
So I am trying to get the result as count from a sql query

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.