joel24 Posted May 30, 2009 Share Posted May 30, 2009 Not sure if this is possible in MySQL alone, however, it will greatly help my cause if it is so I'm pursuing it. Say I have a table with columns like this ID Col1 Col2 1 10 20 2 20 15 3 15 30 Is it possible to find the sum of the greater values of col1 and col2 in MySQL... without using php etc..? So it would be 20 + 20 + 30... Any help would be greatly appreciated! Quote Link to comment https://forums.phpfreaks.com/topic/160249-solved-mysql-sum-of-greater-of-two-columns/ Share on other sites More sharing options...
Hybride Posted May 30, 2009 Share Posted May 30, 2009 I know you can do SELECT MAX(col1)+MAX(col2) FROM table but not sure if that's what you want. You get the max number of column 1 and the max of column 2, then add those two together. Quote Link to comment https://forums.phpfreaks.com/topic/160249-solved-mysql-sum-of-greater-of-two-columns/#findComment-845844 Share on other sites More sharing options...
joel24 Posted May 31, 2009 Author Share Posted May 31, 2009 thanks, but i need it to find the higher of each column in each row.... and add that so if col1 was greater than col2 in row 23 it would put col1 in the addition, and if col2 were greater than col1 in row 12 it would put col1 in the addition there. anyone?! Quote Link to comment https://forums.phpfreaks.com/topic/160249-solved-mysql-sum-of-greater-of-two-columns/#findComment-846070 Share on other sites More sharing options...
Ken2k7 Posted May 31, 2009 Share Posted May 31, 2009 SELECT SUM(IF(col1>col2,col1,col2)) AS sum FROM table; Quote Link to comment https://forums.phpfreaks.com/topic/160249-solved-mysql-sum-of-greater-of-two-columns/#findComment-846072 Share on other sites More sharing options...
joel24 Posted May 31, 2009 Author Share Posted May 31, 2009 ahh wow, didn't know you could use if statements in mysql thank you!! Quote Link to comment https://forums.phpfreaks.com/topic/160249-solved-mysql-sum-of-greater-of-two-columns/#findComment-846166 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.