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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T04:34:01+00:00 2026-05-19T04:34:01+00:00

i have a problem with joining three tables in mysql. lets say we have

  • 0

i have a problem with joining three tables in mysql.

lets say we have a table named posts which I keep my entries in it, i have a table named likes which i store user_id’s and post_id’s in and a third table named comments which i store user_id’s and post_id’s and comment’s text in it.

I need a query that fetches list of my entries, with number of likes and comments for each entry.

Im using this query:

SELECT posts.id, count(comments.id) as total_comments, count(likes.id) as total_likes
FROM `posts`
LEFT OUTER JOIN comments ON comments.post_id = posts.id 
LEFT OUTER JOIN likes ON likes.post_id = posts.id
GROUP BY posts.id

but there is a problem with this query, if comments are empty for an item, likes count is just ok, but lets say if an entry has 2 comments and 4 likes, both total_comments and total_likes will be “8”, meaning that mysql multiplies them.
I’m confused and I dont know what whould I do.

Thanks in advace.

  • 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-19T04:34:01+00:00Added an answer on May 19, 2026 at 4:34 am

    Use count(distinct comments.id) and count(distinct likes.id), provided these ids are unique.

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

Sidebar

Related Questions

I have a problem with joining 2 tables together, as follows Table 'jobs' id
i have problem when joining tables(left join) table1: id1 amt1 1 100 2 200
I have one problem with one SP, when I am joining some specific tables,
I am having a problem sorting results from joining tables that have to be
I've got three tables in a MySql database that I'm joining in a query
I have a problem with tables joining. I have two tables: product_commons and product_prices.
I am new to sql and I have come across a problem on joining
My problem: I have a table with a Channel <int> and a Value <float>
I need some help joining two table. I've got: my_type_table , which has columns:
Given that I have three tables (Customer, Orders, and OrderLines) in a Linq 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.