Welcome to WebmasterWorld Guest from

Forum Moderators: open

Message Too Old, No Replies

Sort by Calculated Values

5:10 pm on Mar 18, 2008 (gmt 0)

5+ Year Member


Right now I am sorting a table for a query by the contents in a column called "col1".

mysql_query("SELECT * FROM table ORDER BY col1 DESC");

Instead, I would like to calculate a "score" to sort the table by.

Somehting where:
score = ((col1-2)*col2)+col3)

Is this possible?


4:52 pm on Mar 19, 2008 (gmt 0)

10+ Year Member

You can do something like ('scuse syntax, I've not tested this):

mysql_query("SELECT ((col1-2)*col2)+col3) AS score, [..other fields..], FROM table ORDER BY score DESC");

6:47 pm on Mar 19, 2008 (gmt 0)

10+ Year Member

you can also sort by position:

SELECT ((col1-2)*col2)+col3) AS score, *
FROM table

8:04 pm on Mar 20, 2008 (gmt 0)

WebmasterWorld Administrator coopster is a WebmasterWorld Top Contributor of All Time 10+ Year Member

At one point wasn't the column position syntax removed from the SQL standard?
9:23 pm on Mar 20, 2008 (gmt 0)

5+ Year Member

Thank you very much, the code worked and helped me emmensly with my script.



Featured Threads

Hot Threads This Week

Hot Threads This Month