Jump to content

Sum Multiple Fields Not Adding Up


kannuk

Recommended Posts

Hey there - how are you? I'm trying to add up multiple fields from a table and it's mostly working but I have one curious error. It always seems to miss the field upgrade6_fee. I'm totally baffled. I thought it was my syntax but I'm not seeing anything wrong with it. Any ideas? Otherwise it all seems to be working.

 

<?php

$query = "SELECT fee1, upgrade3_fee, upgrade4_fee, upgrade5_fee, upgrade6_fee, upgrade7_fee, SUM(fee1 + upgrade3_fee + upgrade4_fee + upgrade5_fee + upgrade6_fee + upgrade7_fee) AS ttotal FROM Register";

$result = mysql_query($query) or die(mysql_error());

 

while($row = mysql_fetch_array($result)){

echo number_format(($row['ttotal']), 2);

}

?>

Link to comment
Share on other sites

I am trying to add up all of the entries from the above fields into one number. I've looked at other examples and this seems to work in theory. Like I said, when I have some of the other fields filled in, it adds up but upgrade6_fee always seems to be missing from the total. So if the grand total is supposed to be 10,000 and there is one entry for upgrade6_fee = 200, the grand total only shows up at 9,800.

 

My table has several hundred entries. The fields I used above are fees paid by the applicant. Fee1 is always filled out but the others may be 0. They are all decimals (00.0) in the table structure.

Link to comment
Share on other sites

Perhaps a sample structure with data would help us solve your issue, along with what you expect the output to be.

 

As far as I can tell, you're selecting a bunch of data you don't use, but I don't know if you actually need that data.

Link to comment
Share on other sites

After testing, that syntax should work fine.

 

--
-- Table structure for table `items`
--

CREATE TABLE IF NOT EXISTS `items` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `val1` int(11) NOT NULL,
  `val2` int(11) NOT NULL,
  `val3` int(11) NOT NULL,
  UNIQUE KEY `id` (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=5 ;

--
-- Dumping data for table `items`
--

INSERT INTO `items` (`id`, `val1`, `val2`, `val3`) VALUES
(1, 2, 4, 5),
(2, 1, 3, 9),
(3, 54, 2, 55),
(4, 34, 3, 4);

 

mysql> SELECT SUM(val1+val2+val3) as total_sum FROM items;
+-----------+
| total_sum |
+-----------+
|       176 |
+-----------+
1 row in set (0.00 sec)

 

Which is correct. I can't tell you why a value is missing, but it's probably user error, and not a problem with your SQL.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.