LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-16-2009, 12:30 AM   #1
secretlydead
Member
 
Registered: Sep 2003
Location: Qingdao, China
Distribution: mandriva, slack, red flag
Posts: 249

Rep: Reputation: 31
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".
 
Old 12-16-2009, 01:11 AM   #2
secretlydead
Member
 
Registered: Sep 2003
Location: Qingdao, China
Distribution: mandriva, slack, red flag
Posts: 249

Original Poster
Rep: Reputation: 31
it's like this:

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


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
PHP MySql search over multiple columns a_m0d Programming 3 07-09-2008 07:19 AM
MySQL: Trying to Drop Columns Named update and delete weibullguy Programming 4 06-13-2008 12:54 PM
compare $php variable to indexed distinct mysql columns secretlydead Programming 1 02-18-2008 10:48 PM
how do I Add multiple columns mysql in postgres Bheki Linux - Software 1 08-28-2003 02:30 PM
MySQL SUM Query zimba Programming 3 03-28-2003 09:55 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:31 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration