homepage Welcome to WebmasterWorld Guest from 54.227.11.45
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Home / Forums Index / Code, Content, and Presentation / Databases
Forum Library, Charter, Moderators: physics

Databases Forum

    
Sort by Calculated Values
itledi




msg:3604243
 5:10 pm on Mar 18, 2008 (gmt 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

 

ytswy




msg:3605465
 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");

syber




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

you can also sort by position:

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

coopster




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

At one point wasn't the column position syntax removed from the SQL standard?

itledi




msg:3606755
 9:23 pm on Mar 20, 2008 (gmt 0)

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

Thanks

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / Databases
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved