LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   mysql sum from one of two columns (https://www.linuxquestions.org/questions/programming-9/mysql-sum-from-one-of-two-columns-775978/)

secretlydead 12-16-2009 12:30 AM

mysql sum from one of two columns
 
How do you get the sum from two columns, selecting only one column at a time by whichever number is smaller?

For example, you have this:
col1, col2
2, 1
1, 2
2, 1

When you do the select sum query, it replies: "3".

secretlydead 12-16-2009 01:11 AM

it's like this:

SELECT SUM( IF( col1 < col2, col1, col2 ) ) AS small_sum FROM table;


All times are GMT -5. The time now is 12:32 PM.