portabletelly Posted June 17, 2007 Share Posted June 17, 2007 Hi, I have a table called call_event. The call_events table has colum called Time_taken. Data in this colum is stored as a decimal vaulue. EG. 4hrs and 15 minutest would be stored as 4.25. I would like to be able to do a mysql query which give a total of all the returned values from a query but Im not quite sure what syntax I need. The query I would like to do is to find out the the total time for a call. I would imagen the query would be somthing like this Query = SELECT Time_Taken FROM call_event WHERE call_id ="$call_ID"; When running this query it will return all the times_taken which have the call_id field = to $call_ID. eg 4.5, 3.25 0.00 5.0 However what do I need to do get the total for all these values eg 4.5 + 3.25 + 0.00 + 5.00 = 12.75 I want to be do a query that will return the value of all the time_Taken added up for example in this case I wanted the query to return 12.75 Is this possible with just a query or do I need to do somthing funky with php to add up all the returned values? Link to comment https://forums.phpfreaks.com/topic/55890-get-totals-for-query/ Share on other sites More sharing options...
soycharliente Posted June 17, 2007 Share Posted June 17, 2007 <?php $query = "SELECT Time_Taken FROM call_event WHERE call_id ='$call_ID'"; $result = mysql_query($query) OR DIE("My code sucks."); $total = 0; if ($result) { while ($r=mysql_fetch_array($result)) { $total += $r["Time_Taken"]; } } return $total; ?> Link to comment https://forums.phpfreaks.com/topic/55890-get-totals-for-query/#findComment-276095 Share on other sites More sharing options...
pocobueno1388 Posted June 17, 2007 Share Posted June 17, 2007 Try this: <?php $query = "SELECT sum(Time_Taken) as TOTAL FROM call_event WHERE call_id ='$call_ID'"; $result = mysql_query($query); $row = mysql_fetch_assoc($result); echo 'The total is <b>'.$row['total'].'</b>'; ?> Link to comment https://forums.phpfreaks.com/topic/55890-get-totals-for-query/#findComment-276096 Share on other sites More sharing options...
soycharliente Posted June 17, 2007 Share Posted June 17, 2007 You didn't have to 1 up me! Link to comment https://forums.phpfreaks.com/topic/55890-get-totals-for-query/#findComment-276097 Share on other sites More sharing options...
pocobueno1388 Posted June 17, 2007 Share Posted June 17, 2007 Lmao, I don't even know if my code will work. It's kinda a shot in the dark xP We'll see. Link to comment https://forums.phpfreaks.com/topic/55890-get-totals-for-query/#findComment-276099 Share on other sites More sharing options...
soycharliente Posted June 17, 2007 Share Posted June 17, 2007 haha, I don't even know if my code will work. It's kinda a shot in the dark xP We'll see. Yeah. I don't test mine either Link to comment https://forums.phpfreaks.com/topic/55890-get-totals-for-query/#findComment-276100 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.