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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T08:12:17+00:00 2026-05-30T08:12:17+00:00

im running a query that usses 3 tabels post, likes and comment i need

  • 0

im running a query that usses 3 tabels “post, likes and comment” i need to get the ammount of likes and comments the post has got, and at the same time get the basic info from the post table so im using the query bellow but the problem is that it copys the value likeAmount to commentAmount if likes is bigger unless comments is 0.

SELECT post.*, COUNT(likes.id) as 'LikeAmount', COUNT(comment.id) as 'commentAmount' FROM post 
  LEFT JOIN likes ON post.id = likes.post
  LEFT JOIN comment ON post.id = comment.post
  GROUP BY post.id 
  ORDER BY LikeAmount DESC"

so that doesnt work but when i add distinct it does work, so when its like this:.

SELECT post.*, COUNT(distinct likes.id) as 'LikeAmount', COUNT(distinct comment.id) as 'commentAmount' FROM post 
LEFT JOIN likes ON post.id = likes.post
LEFT JOIN comment ON post.id = comment.post
GROUP BY post.id 
ORDER BY LikeAmount DESC";

i dont see why it works with distinct and doesnt with out, and does distinct mather performance wise or does it make no diffrence sinds it will be used in a website that has a lott of trafic..

  • 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-30T08:12:18+00:00Added an answer on May 30, 2026 at 8:12 am

    try this, not short, but readable:

    SELECT
        p.*,
        pl.like_count,
        pc.comment_count
    FROM post p
    
    #join likes
    LEFT OUTER JOIN (
        SELECT
            post,
            COUNT(*) AS like_count
        FROM likes
        GROUP BY post
    ) AS pl
        ON pl.post = p.id
    
    #join comments
    LEFT OUTER JOIN (
        SELECT
            post,
            COUNT(*) AS comment_count
        FROM comment
        GROUP BY post
    ) AS pc
        ON pc.post = p.id
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I got this error running a query that goes against 2 tables with combined
I'm running a query that is timing out for first two times and returning
I am running a query that returns records from the LEFT of the EXCEPT
I am running a query that returns XML data and spools to a file.
I have situation, where running a query that filters by an indexed column in
I'm running an MySQL query that returns results based on location. However I have
I recently discovered that a sql query that was running fine earlier is now
I am running a sql in PHP query that is dieing with the mysql_error()
Part of a complex query that our app is running contains the lines: ...(inner
Ok, here's a query that I am running right now on a table that

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.