1

Lets say i have a table that has the following field names

'id' 'likes' 'rating' 'view'

A normal MySQL sort would require a simple

SELECT * FROM $table ORDER BY likes DESC, rating DESC, view DESC

Now lets say I would want to order the given entries in my table by an arbitrary function

function custom_sort(likes, rating, view) {
    fame = 0.5 * like + 0.25 * rating - view;
    return fame;
}

I want to know if it is possible to do this pseudo code like action

 SELECT * FROM $table ORDER BY custom_sort(likes, rating, view) DESC
1
  • 1
    You even can do it without any functions, just write all this (I mean 0.5 * like + 0.25 * rating - view) in ORDER BY clause. Commented Aug 15, 2012 at 9:53

1 Answer 1

1

How about simply...

SELECT * FROM $table ORDER BY ( 0.5 * likes + 0.25 * rating - view ) DESC
Sign up to request clarification or add additional context in comments.

2 Comments

this only works if you use normal math operators like +-/* but what about php functions like min/max?
@toxicate20 Yes, but you should not confuse these functions with the aggregate ones. For example, you can write something like... 0.5 * LEAST(likes, 0.5) in your ORDER BY expression just fine.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.