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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T01:28:16+00:00 2026-05-26T01:28:16+00:00

I have a content items table structured like | contentid | message | categoryid

  • 0

I have a content items table structured like

|  contentid |   message     |    categoryid  |  userid  |    dateadded | etc.. 
     15          foo bar            3             4           somedate
     16          more foo bar       3             4           somedate
     16          foo stuff          3             4           somedate

and a votes table, where direction = 1 = an up vote, and = 2 being a down vote.

|  voteid   |  contentid |  userid  |  direction |    dateadded
    7             15          4            1           some date
    8             15          6            1           some date
    9             15          17           2           some date

And I’d like to select a set of content items, having an additional column on the end with its calculated score based on the votes in the votes table.

Previously, I had a ‘score’ column attached to the content table, and each time a vote was cast, it would update its score. This was done so I wouldnt have to have a more complex query to calculate scores on each SELECT, but I’d like to change this now.

This votes table was designed a while ago, so if changing all the votes values to something other than 1 or 2 (perhaps -1 for a downvote) would make it easier, I will update the entire table.

What would the query be to pull all content items, each with a score in a calculated column?

  • 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-26T01:28:17+00:00Added an answer on May 26, 2026 at 1:28 am

    Assuming the vote “direction” represents up and down votes:

    SELECT i.contentid, 
           SUM(CASE WHEN v.direction = 1 THEN 1
                    WHEN v.direction = 2 THEN -1
                    ELSE 0 END) AS Votes
        FROM items i
            LEFT JOIN votes v
                ON i.contentid = v.contentid
        GROUP BY i.contentid
        HAVING SUM(CASE WHEN v.direction = 1 THEN 1
                    WHEN v.direction = 2 THEN -1
                    ELSE 0 END) > -3
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a bunch of items (text, image, mixed content, etc) that I want
i have this three tables. Table: Item Columns: ItemID, Title, Content, NoChange (Date) Table:
I have the following contents in my XML file: <items> <item id=1><content><![CDATA[<p>string</p>]]></content></item> </items> I
I have content management system application that uses a polymorphic tree table as the
can we have content text directly in <div>text content</div> or it should be like
I have a table full of items from different sources. Some of the sources
I want to implement something like facebook's unread items/notifications. I have the following model
I have a table with dynamically changing rows items. Among the rows there is
I have a module, that is just a list of items. Relating to content.
If you have the following table structure: item: id, content ect item_tags: item_id, tag_id

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.