问题
Any idea on how to order the results of a MYSQL query by the sum of two columns rather than by a single column?
Select * FROM table ORDER BY (col1+col2) desc
I know that won't work., but I hope it conveys what I want to do fairly well.
Thanks!
回答1:
Why not try before concluding it doesn't work? In point of fact, it does.
回答2:
Suppose you have a table named 'Students'

Now you want to know the total marks scored by each student. So, type 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.

回答3:
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
回答4:
The query you wrote should work just fine, you can have any expression in the ORDER BY
clause.
来源:https://stackoverflow.com/questions/3059138/mysql-order-by-sum-of-columns