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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T09:46:29+00:00 2026-06-17T09:46:29+00:00

I’ve got a list of items in my table, with fields votes_up and votes_down

  • 0

I’ve got a list of items in my table, with fields votes_up and votes_down along with others. Now I want to order the items based on popularity which is a function of these two fields. The problem is that algorithm of popularity is dependent on the values of votes_up.

For example, if votes_up is greater 5:

popularity= (votes_up/votes_down)*20

If votes_up is greater than 50:

popularity= (votes_up/votes_down)*15

(I’ve just used this to explain.. the actual algorithm is different).

Now how can I do this, because normally I would do in my query something like this:

ORDER BY (votes_up/votes_down)*20)

But I cannot do this right now because what comes after ORDER BY is dependent on the value of votes_up. Only way I can do this is by:

if(votes_up >5){
    p = (votes_up/votes_down)*20;
} else if(votes_up > 50){
    p = (votes_up/votes_down)*15;
}

Then in my query I can have ORDER BY p.

Now of course this is not valid PHP, but I just want to explain my problem. I hope you understand what I want to achieve.

  • 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-17T09:46:30+00:00Added an answer on June 17, 2026 at 9:46 am

    You could simply calculate your popularity using a case statement and then order based on that. It might look like this:

    SELECT ((votes_up/votes_down)*(CASE WHEN votes_up > 50 THEN 15 WHEN votes_up > 5 THEN 20 ELSE ? END CASE)) as popularity, [ANY OTHER FIELDS]
    FROM table
    ORDER BY popularity DESC
    

    You never stated what do do with votes_up <= 5 so I just put a ? for that case, you can fill in the blank.

    Note however that this will not be able to utilize an index for this sort, so if you have large numbers of rows you expect to return, your query might perform slowly.

    You might want to consider adding a field into the table that stores the calculated popularity value (which would by updated by the application on votes up or down). You could then index this field and get a more optimized sort.

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

Sidebar

Related Questions

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
Configuring TinyMCE to allow for tags, based on a customer requirement. My config is
I have a jquery bug and I've been looking for hours now, I can't
I've got a string that has curly quotes in it. I'd like to replace
I have a small JavaScript validation script that validates inputs based on Regex. I
this is what i have right now Drawing an RSS feed into the php,
I have a French site that I want to parse, but am running into
I want use html5's new tag to play a wav file (currently only supported
I'm interested in microtypography issues on the web. I want a tool to fix:

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.