MYSQL order by sum of columns

Any idea on how to sort the results of a MYSQL query into the sum of two columns, not one column?

Select * FROM table ORDER BY (col1+col2) desc 

I know this will not work. But I hope that he conveys what I want to do well enough.

Thanks!

+6
mysql sql-order-by
source share
4 answers

Why not give it a try before you complete it? Actually it is.

+21
source share

Suppose you have a table named " Students "

enter image description here

Now you want to know the overall grades scored by each student. So enter the following query

 SELECT Name, S1, S2, SUM(S1+S2) AS TOTAL FROM Students GROUP BY Name, S1, S2 ORDER BY Total; 

You will get the following result.

enter image description here

+3
source share

I think you should be able to do

 SELECT *, col1+col2 as mysum ORDER BY mysum 

Which is essentially the same as you already have

+2
source share

The text you requested should work fine, you can have any expression in the ORDER BY .

+1
source share

All Articles