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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T14:19:32+00:00 2026-06-08T14:19:32+00:00

I am trying to retrieve data from three tables at once. The tables look

  • 0

I am trying to retrieve data from three tables at once. The tables look like:

categories

id
category
messageid

messages

id
title
message

comments

id
messageid
message

What I am trying to get is 1 message (because I have a WHERE clause based on id 3), 3 categories (because there are 3 categories linked to the message) and 2 comments (because there are 2 comments linked to the message).

I’m trying to retrieve the data by usnig the following query:

SELECT categories.category, messages.id, messages.title, messages.message, comments.count, comments.id as commentid, comments.message as commentmessage
FROM categories
RIGHT JOIN 
  (SELECT id, title, message
  FROM messages WHERE messaged.id = 3) messages
ON messages.id = categories.messageid
LEFT JOIN
  (SELECT count(id), id, message
  FROM comments
  GROUP BY id, message) comments
ON messages.id = comments.messageid
ORDER BY article.id DESC

However when running this query I get 6 results:

category      id  title      message      count  commentid  commentmessage
test          3   the title  the message  1      6          comment 1
test          3   the title  the message  1      5          comment 2
installation  3   the title  the message  1      6          comment 1
installation  3   the title  the message  1      5          comment 2
question      3   the title  the message  1      6          comment 1
question      3   the title  the message  1      5          comment 2

Where I expected a result something like:

category      id  title      message      count  commentid  commentmessage
test          3   the title  the message  1      6          comment 1
question      3   the title  the message  1      5          comment 2
installation  3   the title  the message  1      null       null

With only three rows I should be able to get all the needed data. Is this even possible? Am I doing it completely wrong ™?

  • 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-06-08T14:19:33+00:00Added an answer on June 8, 2026 at 2:19 pm

    There are a couple issues here as mentioned in the comments.

    First, since you are joining three tables, the answer you are getting is correct. 1 x 2 x 3 rows = 6.

    Second, your aggregate of the comments is not really aggregating anything. As you can see in your results, count is always one, when I expect you thought it would be 2 for two comments. Since you are grouping on the id, the count is performed for each unique id, which will always be one. I think you probably want to group on messageid

    SELECT count(*), messageid
    FROM comments
    GROUP BY messageid
    

    You would need to do another join or a separate query to get the comments themselves.

    Also as discussed in the comments, you wouldn’t typically get the information this way; you would usually just make the three queries since two of the relationships are one to many. You could, if your categories were short (and you’re using SQL Server) compress the categories into their own column (ie. ‘test, installation, question’). Here is how you would do that.

    select id, title, message,
           (select CAST(category + ', ' as nvarchar(max))
            from @Categories c where messageid = m.id
            for xml path('')) as Categories
    from @Messages m
    where m.id = 3
    

    Actually, there are several methods for doing that, but that’s quick and dirty. Then you would only need one additional query for the comments. You could just join to the previous query and get all your information in two rows like this

    select m.id, title, m.message,
           (select CAST(category + ', ' as nvarchar(max))
            from @Categories c where messageid = m.id
            for xml path('')) as Categories,
            cm.message
    from @Messages m
    left outer join @Comments cm on m.id = cm.messageid
    where m.id = 3
    

    but again, you’d probably just want to make an additional query to avoid duplicating information.

    Finally, I wanted to show how you would probably want to do the comment count.

    select m.id, title, m.message,
           (select CAST(category + ', ' as nvarchar(max))
            from @Categories c where messageid = m.id
            for xml path('')) as Categories,
            CommentCount,
            cm.message
    from @Messages m
    left outer join 
    (   
        select messageid, COUNT(*) CommentCount
        from @Comments 
        group by messageid
    ) rsCommentCount on rsCommentCount.messageid = m.id
    

    And lastly, here is a link showing that working.

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

Sidebar

Related Questions

I’m trying to retrieve data from an entity and populate a viewModel property like
I'm trying to retrieve data from two tables, 'publisher' and 'magazine'. I'm listing the
Iam trying to retrieve data from mysql database into stylesheet.php but it is not
I'm trying to retrieve data from an Entity in Core Data where I know
I am trying to retrieve CLOB data from our Oracle database. the code is
I'm trying to retrieve hierarchical data from a table but am failing to do
I'm trying to retrieve record data from an MS-Access database and store it into
I am trying to retrieve specific data from my MySQL database by passing the
I'm trying to use Python 2.7 regex's to retrieve data from sample web pages
I am trying to retrieve data into an xml tag from the database using

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.