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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T00:19:24+00:00 2026-06-11T00:19:24+00:00

I have these tables: users , comments , ratings , and items I would

  • 0

I have these tables: users, comments, ratings, and items

I would like to know if it is possible to write SQL query that basically does this:

user_id is in each table. I’d like a SQL query to count each occurrence in each table (except users of course). BUT, I want some tables to carry more weight than the others. Then I want to tally up a “score”.

Here is an example:

user_id 5 occurs…
2 times in items;
5 times in comments;
11 times in ratings.

I want a formula/point system that totals something like this:

items 2 x 5 = 10;
comments 5 x 1 = 5;
ratings 11 x .5 = 5.5

TOTAL 21.5

This is what I have so far…..

SELECT u.users
     COUNT(*) r.user_id
     COUNT(*) c.user_id
     COUNT(*) i.user_id
FROM users as u
JOIN COMMENTS as c
     ON u.user_id = c_user_id
JOIN RATINGS as r
     ON r.user_id = u.user_id
JOIN ITEMS as i
     i.user_id = u.user_id
WHERE
    ????
GROUP BY u.user_id
ORDER by total DESC

I am not sure how to do the mathematical formula portion (if possible). Or how to tally up a total.

Final Code based on John Woo’s Answer!

$sql = mysql_query("
        SELECT  u.username,
    (a.totalCount * 5) +
    (b.totalCount) +
    (c.totalCount * .2) totalScore
    FROM users u 
    LEFT JOIN
        (
            SELECT user_id, COUNT(user_id) totalCount
            FROM items 
            GROUP BY user_id
        ) a ON a.user_id= u.user_id
    LEFT JOIN
        (
            SELECT user_id, COUNT(user_id) totalCount
            FROM comments
            GROUP BY user_id
        ) b ON b.user_id= u.user_id
    LEFT JOIN
        (
            SELECT user_id, COUNT(user_id) totalCount
            FROM ratings
            GROUP BY user_id
        ) c ON c.user_id = u.user_id
    ORDER BY totalScore DESC LIMIT 10;");
  • 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-11T00:19:25+00:00Added an answer on June 11, 2026 at 12:19 am

    Maybe this can help you,

    SELECT  u.user_ID,
            (a.totalCount * 5) +
            (b.totalCount) +
            (c.totalCount * .2) totalScore
    FROM    users u LEFT JOIN
                (
                    SELECT user_ID, COUNT(user_ID) totalCount
                    FROM items 
                    GROUP BY user_ID
                ) a ON a.user_ID = u.user_ID
            LEFT JOIN
                (
                    SELECT user_ID, COUNT(user_ID) totalCount
                    FROM comments
                    GROUP BY user_ID
                ) b ON b.user_ID = u.user_ID
            LEFT JOIN
                (
                    SELECT user_ID, COUNT(user_ID) totalCount
                    FROM ratings
                    GROUP BY user_ID
                ) c ON c.user_ID = u.user_ID
    ORDER BY totalScore DESC
    

    but based on yur query above,thismay also work

    SELECT  u.users
            (COUNT(*) * .5) +
            COUNT(*) +
            (COUNT(*) * 2) totalcore
    FROM users as u
            LEFT JOIN COMMENTS as c
                ON u.user_id = c_user_id
            LEFT JOIN RATINGS as r
                ON r.user_id = u.user_id
            LEFT JOIN ITEMS as i
                ON i.user_id = u.user_id
    GROUP BY u.user_id
    ORDER by totalcore DESC
    

    The only difference is by using LEFT JOIN. You will not use INNER JOIN in this situation because there are chances that user_id is not guaranteed to exists on every table.

    Hope this makes sense

    enter image description here

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

Sidebar

Related Questions

I have these tables: comments uid id pid pages pid user users id rank
I have an ASP.NET app that accepts users comments and them in a SQL
Hi, I have these two tables: users and friends (friend_status = 1 means the
Hi, I have these two tables: users and friends (friend_status = 1 means the
I have these tables: USERS: user_ID username password email data_registered POSTS: post_ID user_ID post_title
I have an array of strings: [users, torrents, comments] these strings are the names
I have 3 tables: users, images (foreign key to user_id), comments (foreign keys: user_id
I have these tables: table_a user_id article_id created_at articles user_id created_at ... I need
I have users table. There are three other tables: developers, managers, testers. All of
Requirements : I have a table of several thousand questions. Users can view these

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.