How to ORDER BY a SUM() in MySQL?

后端 未结 5 815
暗喜
暗喜 2020-11-27 19:12

I have a table: \"ID name c_counts f_counts \"

and I want to order all the record by sum(c_counts+f_counts) but this doesn\'t work:

SELECT

5条回答
  •  野性不改
    2020-11-27 19:41

    This is how you do it

    SELECT ID,NAME, (C_COUNTS+F_COUNTS) AS SUM_COUNTS 
    FROM TABLE 
    ORDER BY SUM_COUNTS LIMIT 20
    

    The SUM function will add up all rows, so the order by clause is useless, instead you will have to use the group by clause.

提交回复
热议问题