Jump to content

MYSQL order by two columns??

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
2 replies to this topic

#1
bbqroast

bbqroast

    Codecall Addict

  • Members
  • PipPipPipPipPipPipPip
  • 554 posts
In my new site I have a database of tutorials...
Right now they are ordered on their Pos Vote
The problem with this is if i had two tutorials:
A. A new one with 5+ votes and 1- vote (looking like a good tutorial so far)
B. A old one with 23+ votes and 128- votes (a terribly written tutorial)
Guess which one would come on top B.
My idea was to take Neg Votes From Pos Votes so B. would be below all tutorials with more than -105 votes and A would be above tutorials with 4 or less votes and below ones with 6 or more.

Here is what i have:
"SELECT * FROM codingtuts ORDER BY plusrating DESC LIMIT $start_from, 10"
$start_from and 10 is how many tuts to display (ignore it, if you want to know this i write a tutorial its being moderated right now).
As you can see it currently runs on how many Pos Votes (plusrating).
Please, write clearly with proper structure. Double spacing makes the text feel un-jointed, Capitalizing Every Word Means People Stop Before Every Word Sub-Consciously Which Is A Pain In The Backside, and use code tags! (The right most styling box).

#2
DEViANT

DEViANT

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 358 posts
--- Edited ---

:D You should rep+ me so that I can win :D

My Blog | Ask me!
Error : Satan did it

#3
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,124 posts
Yum, you can use a nice long query to do the math (assuming negrating is your negative ratings):
SELECT tutid, plusrating, negrating, (plusrating - negrating) AS net FROM codingtuts ORDER BY net DESC LIMIT $start_from, 10
Denormalizing your database by adding another table for the results would be even faster, but it is regarded as bad practise. I would stick with just making net rating another column.
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.