Suppose there is a rate sign and five columns in it. The first column stores the names, and the rest some numbers. What to indicate in ORDER BY in order to display in order those entries whose sum of these numbers is greater?

 INSERT INTO `rating_uni` (`id`, `name`, `r1`, `r2`, `r3`, `r4`, `r5`) VALUES (47, 'Name1', 0, 2, 1, 1, 1), (48, 'Name2', 0, 2, 2, 1, 0), (49, 'Name3', 1, 0, 1, 1, 0), (50, 'Name4', 0, 0, 0, 0, 0), (51, 'Name5', 3, 0, 0, 0, 1), (52, 'Name6', 0, 0, 0, 1, 1), (46, 'Name7', 0, 0, 0, 0, 0), (53, 'Name8', 0, 0, 0, 0, 0), 

Here is an example: for id 47, the total is 0 + 2 + 1 + 1 + 1 = 5, for the id 48, the total is 0 + 2 + 2 + 1 + 0 = 5, for id 49, the total is 1 + 0 + 1 + 1 + 0 = 3. Sorting should occur on these sums.

    3 answers 3

     SELECT * FROM rating_uni ORDER BY r1+r2+r3+r4+r5 
       SELECT Name, Column1, Column2, Column3, Column4, Column5, SUM(Column1 + Column2 + Column3 + Column4 + Column5) AS Total FROM MYTABLE ORDER BY Total; 
      • in this case, sql counts absolutely all values ​​and displays their sum. I need to calculate the amount from each line separately - giorgi ksovrela
       SELECT Name, Column1, Column2, Column3, Column4, Column5, (select SUM(Column1 + Column2 + Column3 + Column4 + Column5) from MyTable mt where mt.id=MYTABLE.id) AS Total FROM MYTABLE ORDER BY Total; 
      • Yes, thanks) - giorgi ksovrela
      • And what for he needs this amount in the output set? moreover received through the correlated subquery? - Akina