Reputation: 195
I have 4 columns each containing a number. I need the echo
to output in such an order, that the row with the biggest sum of numbers (from each of the 4 columns) is the first.
For example:
1)id1|1 |10|1 |2|
2)id2|3 |12|43|1|
3)id3|12|0 |1 |1|
After ordering:
1)id2|3 |12|43|1
2)id1|1 |10|1 |2
3)id3|12|0 |1 |1
One option is to make a "sum" column... but that would require updating it each time one of the numbers in one of the columns changes... Is there a more simple solution?
For example, I output the sum of numbers of each id
when it is displayed in a single <div>
, I add them with a function before outputing the <div>
.
Upvotes: 1
Views: 71
Reputation: 8662
SELECT * FROM your_table ORDER BY (column1+column2+column3+column4) DESC
will get it done
Upvotes: 4
Reputation: 12037
You could use this mysql statement:
SELECT (row1 + row2 + row3 + row4) sum FROM your_table
Upvotes: 0