jimbeeer
jimbeeer

Reputation: 1695

mysql php sort by highest of 3 separate fields

I've got a table with 3 separate scores in 3 separate fields:

User / Score 1 / Score 2 / Score 3
Person 1: 10 21 7
Person 2: 17 4 20
Person 3: 1 5 22

Is there a mysql command that will effectively sort each person out by the highest score from the 3 fields.

So here I need it to return:

Person 3: 1 5 22
Person 1: 10 21 8
Person 2: 17 4 20

The only way I can think of doing it would be to put them in an array, check each number for each person against each other to find the highest, then sort them into a different array.

This seems very long-winded and labour intensive though.

Upvotes: 1

Views: 29

Answers (1)

MrTux
MrTux

Reputation: 33993

Add order by greatest(score1,score2,score3) desc

Manual for GREATEST()

Upvotes: 1

Related Questions