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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T19:23:27+00:00 2026-06-09T19:23:27+00:00

I have three tables that are joined. I almost have the solution but there

  • 0

I have three tables that are joined. I almost have the solution but there seems to be one small problem going on here. Here is statement:

SELECT items.item,
COUNT(ratings.item_id) AS total,
COUNT(comments.item_id) AS comments,
AVG(ratings.rating) AS rate
FROM `items`
LEFT JOIN ratings ON (ratings.item_id = items.items_id)
LEFT JOIN comments ON (comments.item_id = items.items_id)
WHERE items.cat_id = '{$cat_id}'  AND items.spam < 5
GROUP BY items_id ORDER BY TRIM(LEADING 'The ' FROM items.item) ASC;");

I have a table called items, each item has an id called items_id (notice it’s plural). I have a table of individual user comments for each item, and one for ratings for each item. (The last two have a corresponding column called ‘item_id’).

I simply want to count comments and ratings total (per item) separately. With the way my SQL statement is above, they are a total.

  • note, total is the total of ratings. It’s a bad naming scheme I need to fix!

UPDATE: ‘total’ seems to count ok, but when I add a comment to ‘comments’ table, the COUNT function affects both ‘comments’ and ‘total’ and seems to equal the combined output.

  • 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-09T19:23:28+00:00Added an answer on June 9, 2026 at 7:23 pm

    Problem is you’re counting results of all 3 tables joined. Try:

    SELECT i.item,
    r.ratetotal AS total,
    c.commtotal AS comments,
    r.rateav AS rate
    FROM items AS i
    LEFT JOIN
        (SELECT item_id, 
        COUNT(item_id) AS ratetotal, 
        AVG(rating) AS rateav 
        FROM ratings GROUP BY item_id) AS r 
    ON r.item_id = i.items_id
    LEFT JOIN
        (SELECT item_id, 
        COUNT(item_id) AS commtotal 
        FROM comments GROUP BY item_id) AS c
    ON c.item_id = i.items_id
    WHERE i.cat_id = '{$cat_id}'  AND i.spam < 5
    ORDER BY TRIM(LEADING 'The ' FROM i.item) ASC;");
    

    In this query, we make the subqueries do the counting properly, then send that value to the main query and filter the results.

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

Sidebar

Related Questions

I have one Linq to SQL query that simply joins three tables and then
I have three tables (people, clients_people, clients) that would be joined using SELECT *
I have two tables that get joined regularly. Table One is about 1 Million
Lets say that I have three tables, customers, orders and orderDetails. I'm doing this:
Need help with a query that I wrote: I have three tables Company id
I have a MySQL query that: gets data from three tables linked by unique
I have a Microsoft SQL Server 2008 query that returns data from three tables
Background In a PostgreSQL 9.0 database, there are various tables that have many-to-many relationships.
I have several tables that get JOINed together to form a table with columns
I have joined three tables into a view, and then i try to update

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.