Welcome to WebmasterWorld Guest from 107.21.163.40

Forum Moderators: open

Message Too Old, No Replies

Sort by Calculated Values

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

Junior Member

5+ Year Member

joined:Dec 5, 2007
posts:107
votes: 0


Hello,

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?

Thanks

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

Preferred Member

10+ Year Member

joined:June 25, 2002
posts:466
votes: 0


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)

Preferred Member

10+ Year Member

joined:Nov 12, 2004
posts:393
votes: 0


you can also sort by position:

SELECT ((col1-2)*col2)+col3) AS score, *
FROM table
ORDER BY 1 DESC

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

Administrator

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

joined:July 31, 2003
posts:12541
votes: 1


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

Junior Member

5+ Year Member

joined:Dec 5, 2007
posts:107
votes: 0


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

Thanks