Jump to content

MYSQL Query - Ordering a selection by difference of two tables (up/down votes)


Recommended Posts

Hello. I would really appreciate some help. Thanks!

 

Background:

This is a table containing photos. Now I would like to do a query where I order the result by 'upvotes(up)' minus(-) 'downvotes(down)' .

 

Table 'photos'
id
cat_id
type_id
title
status
dateposted	
up (int)
down(int)

At the moment I am just able to sort it by the popularity of the 'upvotes' .. see below:

$sql = "Select photos.*, categories.cat FROM videos, categories
WHERE photos.cat_id = categories.id AND photos.status ='1' ORDER BY photos.up DESC
LIMIT $start, $limit";
$result = mysql_query($sql);

 

If a photo has more up-votes than down-votes it will be a positive number.

If a photo has more down-votes than up-votes it will be a negative number.

 

And I would like to sort the query running from the highest positive number to the largest negative number.

 

I am very puzzled on how to solve this and would very much appreciate some help. Cheers /df

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.