Re: Q: Sorting by the sum of columns?
ZeldorBlat wrote:
> On May 12, 3:44 pm, genzeryk <genze...@gmail.com> wrote:
>> Hi Folks!
>>
>> I'm a newbie MySQL programmer. While doing my project I have faced
>> following problem:
>>
>> I have five columns in my table (indexed 'id' and four with numerical
>> values). I would like to sum the numerical values into a kind of
>> virtual column and sort the table accordingly to the decreasing
>> values of the sum (of the virual column).
>>
>> I have searched in numerous tutorials and FAQs but have not found
>> description of even similar situation.
>>
>> Please help
>>
>> Best regards,
>>
>> Genz
>
> select id, (a + b + c + d) theSum
> from someTable
> order by (a + b + c + d) desc
That seems needlessly wasted on resources, by summing the columns twice.
Surely either of these 2 would be better:
SELECT
`id`,
(`a` + `b` + `c` + `d`) `theSum`
FROM `someTable`
ORDER BY `theSum`
or
SELECT
`id`,
(`a` + `b` + `c` + `d`) `theSum`
FROM `someTable`
ORDER BY 2
|