Jump to content

SQL SUM with array?


mattichu

Recommended Posts

Hi!

 

i have a table:

 

 

 

Day        Username          Rate      Hours        Shift Cost

--------------------------------------------------------------------

MON          Matt                  8.5            2                £17

MON        Imogen                6              2                £12

MON          Matt                  8.5            1                £8.5

MON        Imogen              6              1                  £6

--------------------------------------------------------------------

 

 

 

Im trying to output a table that shows the total cost for each user.

 

like:

 

 

User        Rate    Hours    Wage

----------------------------------------

Matt        8.5        3        £25.5

Imogen    6          3        £18

----------------------------------------

 

 

How would i go about doing this??

 

many thanks!

 

x

Link to comment
Share on other sites

Moving to MySQL to get the query first.

 

Not sure what you mean by "with array" but you have to SUM() "hours" and "Shift Cost" and GROUP BY() "Username".  '£' may give you some trouble.  Try it yourself and post what you come up with.

Link to comment
Share on other sites

SOLVED

 

<?


$username="****";
$password="*****";
$database="******";
mysql_connect('localhost',$username,$password);
@mysql_select_db($database) or die( "Unable to select database");
$query="select username,rop, sum(totalcost) as totalcost, sum(totalhours) as totalhours from hours group by username";	 
$result = mysql_query($query) or die(mysql_error());

// Print out result
while($row = mysql_fetch_array($result)){

?>
<html>
<table><td><?php echo $row['username']; ?></td><td><?php echo $row['rop'] ; ?></td><td><?php echo $row['totalhours'] ; ?></td><td><?php echo $row['totalcost'] ; ?></td>
</table>
</html>
<?	
}
?>

 

many thanks guys!

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.