Greetings. The mysql database has a table with the following columns: id (primary), value1 , value2 , value3 ... So, I want to get data sorted by the sum of the value columns, that is, as if ORDER BY totalValues DESC . Of course, I can create an additional column "totalValues", which will need to be updated with each change of any value , but columns that directly depend on others - as I understand it, are not good. And yes, I can then sort the data inside my PHP script, but can I get ready, sorted data? Are there any elegant solutions to this question?
|
1 answer
In Order by, write the expression you want to sort.
Here is an example:
CREATE TABLE valueTable (`id` int, value1 int, value2 int, value3 int); INSERT INTO valueTable (`id`, `value1`, `value2`, `value3`) VALUES (1, 2,3,4), (2, 4,4,4), (3, -2,-3,-4); and request
SELECT * FROM valueTable order by (value1 + value2 + value3); and the result
id value1 value2 value3 3 -2 -3 -4 1 2 3 4 2 4 4 4 - oneThanks, did not suspect that
order byworks with expressions. - Nik
|
select * from table order by (value1+value2+value3)- SaidolimORDER BYis performed for the field, is it possible to do this for the sum of the fields? - Nik