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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T04:25:29+00:00 2026-05-26T04:25:29+00:00

I have a left join to a table and want to count columns from

  • 0

I have a left join to a table and want to count columns from it, after grouping by a column of the parent table:

SELECT * , COUNT(list.id) AS listcount, COUNT(uploads.id) AS uploadcount 
FROM members 
LEFT JOIN lists ON members.id= list.mid
LEFT JOIN uploads ON members.id= uploads.mid
GROUP BY members.id

Assume that a user can have either lists or uploads based on the type of user. Then is above query good enough? If not why?

Or do I have to use this query?

SELECT * , l.listcount,   u.uploadcount 
FROM members 
LEFT JOIN (select count(lists.id) as listscount,mid from lists group by mid) as l
                                                                on l.mid = m.id
LEFT JOIN (select count(uploads.id) as uploadscount
                           ,mid from uploads group by mid) as u on u.mid = m.id
GROUP BY members.id

Or correlated subqueries?

SELECT *,
      (select count(lists.id) as listscount from lists as l where l.mid = m.id
       group by mid) as listcount
      (select count(uploads.id) from uploads as u where u.mid = m.id
       group by mid) as uploadscount 
FROM members 
GROUP BY members.id

And which is best solution?

  • 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-26T04:25:30+00:00Added an answer on May 26, 2026 at 4:25 am

    The alias m for members is missing in query 2 and 3. Otherwise they should give the same numbers.

    Query 2 (fixed) will perform fastest.

    Query 1 is different in that it will give a higher number for uploads, if there are cases of multiple lists per member. After joining to lists, there will be multiple rows for a member too, which will increase the count for uploads. So query 1 is probably wrong.

    Also, NULL values are not counted. The manual informs:

    COUNT(expr)

    Returns a count of the number of non-NULL values of expr in the rows
    retrieved by a SELECT statement. The result is a BIGINT value.

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

Sidebar

Related Questions

i have this SELECT COUNT(1) cnt, a.auther_id FROM `posts` a LEFT JOIN users u
I have the following query SELECT e.topicShortName, d.catalogFileID, e.topicID FROM catalog_topics a LEFT JOIN
I have two tables which I want to join together using a left outer
I want to have a 2 column layout, and have the left column able
I want to find items in common from the following_list column in a table
I'm getting odd results from a MySQL SELECT query involving a LEFT JOIN ,
I want to create a MATERIALIZED VIEW from a LEFT JOIN of 2 tables.
I want to select all users from my table where access_level_id!=0. I also want
I have a MySQL Left Join problem. I have three tables which I'm trying
We have the query below. Using a LEFT OUTER join takes 9 seconds to

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.