| Welcome to WebmasterWorld Guest from 22.214.171.124 |
register, free tools, login, search, subscribe, help, library, announcements, recent posts, open posts,
|Pubcon Platinum Sponsor 2014|
|Sort by Calculated Values|
| 5:10 pm on Mar 18, 2008 (gmt 0)|
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.
score = ((col1-2)*col2)+col3)
Is this possible?
| 4:52 pm on Mar 19, 2008 (gmt 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)|
you can also sort by position:
SELECT ((col1-2)*col2)+col3) AS score, *
ORDER BY 1 DESC
| 8:04 pm on Mar 20, 2008 (gmt 0)|
At one point wasn't the column position syntax removed from the SQL standard?
| 9:23 pm on Mar 20, 2008 (gmt 0)|
Thank you very much, the code worked and helped me emmensly with my script.
All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved