Forum Moderators: coopster

Message Too Old, No Replies

ORDER BY Methods

         

capulet_x

6:08 pm on Apr 26, 2007 (gmt 0)

10+ Year Member



Is there a way to ORDER BY the SUM of two or more fields in a row; to exress that in a single query... or must you first sum the fields per row using a standalone script, post that value in the row in its own distinct field and then ORDER BY that value?

Does that make sense to anyone?

ericjust

6:26 pm on Apr 26, 2007 (gmt 0)

10+ Year Member



SELECT * FROM table t
ORDER BY (t.field1 + t.field2) ASC;

capulet_x

10:42 pm on Apr 26, 2007 (gmt 0)

10+ Year Member



Nice...

Thanks Eric.

Okay I've been experimenting and something else occured to me:

Is it possible to use ORDER BY with a condition like...

ORDER BY `years-in-college` DESC WHERE (or WHEN) something.

What I'm trying to do is see if I can sort query results by something and then sort them by some other criteria like alphbetical order.

Actually maybe something like:

"SELECT `co-workers`, SUM(`years-in-college` + `years-with-job`/ age) AS totalexperience FROM myoffice GROUP BY age ORDER BY totalexperience DESC";

Does that make sense?

Would the results be listed first by age and then by totalexperience?

ericjust

11:00 pm on Apr 26, 2007 (gmt 0)

10+ Year Member



SELECT `co-workers`, (`years-in-college` + `years-with-job` / age) AS totalexperience
FROM myoffice
ORDER BY age ASC,totalexperience DESC;

capulet_x

11:25 pm on Apr 26, 2007 (gmt 0)

10+ Year Member



Eric,
You rock!

ericjust

12:26 am on Apr 27, 2007 (gmt 0)

10+ Year Member



I'm glad I could help. Cheers!