Getting Sum on multiple fields in one Select
Posted: Wed Jan 05, 2011 2:48 pm
I have several columns that I want the sum of based on a criteria. I want to place these individual sums into variables. Is it possible to do this in one Select statement. Here is a portion of my code.
Notice the select statement is the same, but I need the sum of each of these fields in a variable. I have more columns to get sum on, but don't want to have so many sql statements if not necessary.
Code: Select all
$stquery = "Select sum(ts_calc_st) from timesheets where ts_insp_id = $inspid and ts_wo_num = '$tswo' and ts_status = 'A' and ts_date >= '$fmdate' and ts_date <= '$todate'";
$strow = mysql_query($stquery);
$sttot = mysql_result($strow,0);
$otquery = "Select sum(ts_calc_ot) from timesheets where ts_insp_id = $inspid and ts_wo_num = '$tswo' and ts_status = 'A' and ts_date >= '$fmdate' and ts_date <= '$todate'";
$otrow = mysql_query($otquery);
$ottot = mysql_result($otrow,0);