Welcome Guest, Not a member yet? Register   Sign In
Filter sql result
#1

[eluser]brian88[/eluser]
Currently I am displaying posts ordering by likes.

1st post - 10 likes and 8 dislikes

2nd post - 8 likes and 1 dislikes

Code:
select *
from posts
order by likes desc

Is there a way to make the "2nd post" display on top of my "1st post" using some sql math or php function.

Im not totally sure on the how I want the posts to display, but if the post has almost as many likes as dislikes I want to make that post drop in rankings.
#2

[eluser]brian88[/eluser]
I assume this is a lot harder than i thought? anyone?
#3

[eluser]Denzel[/eluser]
So you want to order the results by the difference in likes and dislikes, at least that's how I understand it. If that's the case, then it's a rather easy query, you were halfway there:
Code:
SELECT * FROM posts ORDER BY (likes - dislikes) DESC
This query will produce the following hypothetical output:
Code:
8 likes 0 dislikes
8 likes 1 dislikes
5 likes 0 dislikes
10 likes 8 dislikes
2 likes 2 dislikes
Notice that the ordering is based upon the difference of likes and dislikes.




Theme © iAndrew 2016 - Forum software by © MyBB