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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T02:51:34+00:00 2026-05-26T02:51:34+00:00

I have 3 different tables: Messages, Comments and Avatar. What I would like to

  • 0

I have 3 different tables:

Messages, Comments and Avatar.

What I would like to do is the following:
Get all Messages in the database, counting for each message the comments and showing the avatar of each user.

This is how the tables are built:

Messages -> messages_id, user_id, title, message

Comments -> comments_id, messages_id, comment

Avatar -> avatar_id, user_id, avatar

I tried the following:

SELECT *, COUNT(comments.comments_id) AS commentCount
FROM messages
LEFT JOIN comments ON messages.messages_id = comments.messages_id
LEFT JOIN avatar on messages.user_id = avatar.user_id

But I get only one row for the messages, the rest is not visible.

Any idea what I’m doing wrong here?

  • 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-26T02:51:35+00:00Added an answer on May 26, 2026 at 2:51 am

    You need to use the GROUP BY:

      SELECT messages_id
           , title
           , message
           , user_id
           , avatar_id
           , avatar
           , count(*) as commentCount
        FROM messages
       inner join avatar on messages.user_id = avatar.user_id
        left join comments on messages.messages_id = comments.messages_id
    GROUP BY messages_id
    

    This should work if:

    • messages_id is unique in messages
    • user_id is unique in avatar

    Otherwise you need to specify the value you want to get.

    Edited:

    I wrote inner join for avatartable thinking in that all users have an avatar. If this is not true should be left join like in your query.

    Second try

    Maybe the error was that the group by should be messages.messages_id instead of messages_id. In fact in others RDMBS this is an error:

    ERROR: column reference “messages_id” is ambiguous

    I’m going to be more precise:

      SELECT m.messages_id    as id
           , min(m.title)     as title
           , min(m.message)   as message
           , min(m.user_id)   as user_id
           , min(a.avatar_id) as avatar_id
           , min(a.avatar)    as avatar
           , count(c.comments_id) as commentCount
        FROM messages as m
        left join avatar as a on m.user_id = a.user_id
        left join comments as c on m.messages_id = c.messages_id
    GROUP BY m.messages_id
    

    All the mincould be deleted in MySQL if you are sure there is only one value. In standard SQL you must choose the value you want, if there is only one you can type min or max.

    I change join with avatar to be left join. Probably not all users have an avatar.

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

Sidebar

Related Questions

I have two different tables, lead and click. I would like to query MySQL
I have four entities that I would like to translate into database tables via
I have three different users tables, and I would like to know what is
I have an existing database with users and administrators in different tables. I am
I've 3 different database tables that have the same 5 fields but those does
I have two tables which i would like to turn into one in a
I have two different tables from which I need to pull data blogs which
I have 4 different tables that I want to join. The tables are structured
I have four different tables, one main SPECIAL table containing only id's that reference
I have a csv file from which I have to populate different tables in

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.