Jump to content

UPDATE with SUM(column)


hilltopper06

Recommended Posts

I am trying to update records using the SUM function, but NULL is being returned (even though I know that values should have been found).  Here is my code:

 

$connection = mysql_connect($host,$user,$pass) or die (mysql_errno().": ".mysql_error()."<BR>");

mysql_select_db($dbname);

$sql = "update pdreg a, pdprog b SET a.PDHours = b.Hours WHERE a.ProgNum = b.ProgNum";

mysql_query($sql);

$sql = "update pdedu a, pdreg b SET a.PDHours = SUM(b.PDHours) FROM pdreg WHERE a.TeaSSN = b.TeaSSN";

mysql_query($sql);

mysql_close($attn);

 

The first query sets the field PDHours (in table pdreg) to the appropriate number of hours as seen in the pdprog table.

 

The second query is supposed to total the hours in the pdreg.PDHours table and have pdedu.PDHours equal to it, where the TeaSSN's match up.  The problem is that I cannot get the SUM function to work right with the WHERE statement.  Can anyone shed any light on this problem?  Thanks.

Link to comment
https://forums.phpfreaks.com/topic/159759-update-with-sumcolumn/
Share on other sites

Ok, I did some messing around in the MySQL Console and this command is returning what I am looking for:

 

SELECT SUM(PDHours) FROM pdreg GROUP BY TeaSSN;

 

It returns a long list of the total hours for each teacher.  Now I am not sure how to take that information and have it update the appropriate field in the pdedu table.  Any ideas?

  • 1 month later...

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.