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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T07:16:43+00:00 2026-05-24T07:16:43+00:00

I’ve got a weird question. I have a user table where each user has

  • 0

I’ve got a weird question. I have a user table where each user has a unique user id. Users are able to post comments (tips) which are saved to a different table, each comment row also has a unique ID and also contains the user ID of the user that posted it. Finally, other users are able to vote on the comments. Votes are stored in another table along with the user ID of the person that voted, and the comment ID of the comment being voted on (no unique ID).

I calculate a person’s rating as the number of up votes minus the number of down votes, so 4 up and 2 down would result in a rating of 2, simple. I want to have a member list that is sortable by a person’s rating, but I’m having a bit of an issue.

I use COUNT() and CASE to determine the number of up and down votes:

(COUNT(CASE WHEN V.vote = 1 THEN 1 ELSE NULL END)-COUNT(CASE WHEN V.vote = 0 THEN 1 ELSE NULL END)) AS 'rating',

This works when I supply a single User ID to the query, in the WHERE clause, WHERE user_id = 1. However, I want to get a list of all members and their ratings, and that’s where the problem happens. My query:

SELECT DISTINCT
  U.user_id,
  (COUNT(CASE WHEN V.vote = 1 THEN 1 ELSE NULL END)-COUNT(CASE WHEN V.vote = 0 THEN 1 ELSE NULL END)) AS 'rating',
  username
  FROM users U
    LEFT JOIN tips T ON U.user_id = T.user_id
    LEFT JOIN votes V ON T.tip_id = V.tip_id

Basically, this should find all rows in the votes where the tip_id is equal to the tip_id in the tips table, then all rows in the tips table where the user_id is equal to the user_id in the users table. The problem I’m having is that I only get a single row returned, even though there are 83 rows in the users table. There are 287 rows in the votes table, with 257 up votes and 30 down votes. This single row that I get has a rating of 227 (which is 257-30), which means that ALL the rows in the votes table are being counted, not just those for the specific user_id that I’m looking for. I want to get the number of up- and down-votes that each user has had for all of his or her tips.

Basically, for every user_id in the users table, it should find all of the tip_id’s for that user id, then all votes for those tip_id’s.

Is this not possible, or am I missing something horribly obvious?

EDIT: I should note that if I remove the COUNT() line completely, then the query works, returns the proper number of rows…it just doesn’t have the vote information.

  • 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-24T07:16:44+00:00Added an answer on May 24, 2026 at 7:16 am
    SELECT 
      U.user_id,
      (COUNT(CASE WHEN V.vote = 1 THEN 1 ELSE NULL END)-COUNT(CASE WHEN V.vote = 0 THEN 1 ELSE      NULL END)) AS 'rating',
     username
     FROM users U
      LEFT JOIN tips T ON U.user_id = T.user_id
      LEFT JOIN votes V ON T.tip_id = V.tip_id
     GROUP BY U.user_id, rating, username
    

    I didn’t test it. But i think you’re missing the group by clause.

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

Sidebar

Related Questions

Basically, what I'm trying to create is a page of div tags, each has
I've got a string that has curly quotes in it. I'd like to replace
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a jquery bug and I've been looking for hours now, I can't
this is what i have right now Drawing an RSS feed into the php,

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.