Gossamer Forum
Home : General : Databases and SQL :

MYSQL Sorting by a calculated variable

Quote Reply
MYSQL Sorting by a calculated variable
Hello.

Is it possibly to sort a query using a calculated variable? Let's say I have a table with the following fields. Name, Number1, Number2. What if I want to display all 'Name' values sorted by Number1 + Number2 (sorted by their sum) or perhaps Number2 / Number1. Is that possible? And how would I do that?

Thanks,
- shazow
Quote Reply
Re: [shazow] MYSQL Sorting by a calculated variable In reply to
Try:

SELECT field1, field2 FROM table ORDER BY field1 + field2

or:

SELECT field1, field2, field1 + field2 as field3 FROM table ORDER BY field3

TheStone.

B.
Quote Reply
Re: [TheStone] MYSQL Sorting by a calculated variable In reply to
Thank you very much! It worked perfectly.