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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T15:41:22+00:00 2026-05-25T15:41:22+00:00

I am trying to get the required result from the following query but it

  • 0

I am trying to get the required result from the following query but it doesnt seem to work…

SELECT DISTINCT
u.user_name as user_name,
u.total_points as total_points,
u.user_id as user_id,
COUNT(a.id) as user_total_articles_published,
COUNT(r.id) as user_total_replies_published,
COUNT(v.id) as user_total_votes_done
FROM users as u
LEFT JOIN articles as a ON u.user_id=a.user_id
LEFT JOIN replies as r ON u.user_id=r.user_id
LEFT JOIN votes as v ON u.user_id=v.user_id
GROUP BY u.user_id
ORDER BY u.total_points DESC
LIMIT 10

If i remove the last 2 LEFT jOINS the query will work… whats wrong with the other 2? Do i have to use another method for this to work?

thanks

  • 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-25T15:41:23+00:00Added an answer on May 25, 2026 at 3:41 pm

    I think by ‘not working’ you mean that the query returns too many records? That is because of the combination of joins. You return each reply for each article record, so the numbers are multiplied. You can solve this by using DISTINCT in the COUNT. That way, you count the unique id’s, so you count each article only once:

    COUNT(distinct a.id) as user_total_articles_published,
    COUNT(distinct r.id) as user_total_replies_published,
    COUNT(distinct v.id) as user_total_votes_done
    

    [edit]

    A possibly faster solution, eliminating the need for DISTINCT and GROUP BY:

    SELECT
      u.user_name as user_name,
      u.total_points as total_points,
      u.user_id as user_id,
      (SELECT COUNT(a.id) FROM articles a 
       WHERE a.user_id = u.user_id) as user_total_articles_published,
      (SELECT COUNT(r.id) FROM replies r 
       WHERE r.user_id = u.user_id) as user_total_replies_published,
      (SELECT COUNT(v.id) FROM votes v 
       WHERE v.user_id = u.user_id) as user_total_votes_done
    FROM users as u
    ORDER BY u.total_points DESC
    LIMIT 10
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Problem I have a YQL query result that I'm trying to get converted and
I'm trying to run the following query in SQLite 3: SELECT *, DISTANCE(latitude, longitude,
i am trying to get the state of the required field validators in an
I'm trying get the visible portion of UIImage from an UIImageView . UIImageView takes
I am trying get Struts 2 and Tiles to work and I am using
I'm trying to get the response from database for further usage.eg: userID However, instead
i'm trying an example of javamail and i get the following error: Exception in
I am trying to use gmail smtp using node_mailer . I get following error
I am trying to make a call to get data from Google Analytics. <?php
I'm trying to get a stored procedure to work for a co-worker who is

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.