php - MySql getting sum: one table contains values, in other tables is rest of data -
i have 3 tables.
first table(items) has: item_id, item_name, , item_value.
second table(handouts) contains: handout_id, handout_item_id, month, vol_id, receiver_name.
third table(volunteer) contains volunteer_id, volunteer_name
i need output monthly sum of items value, per volunteer selected month. here have far(what stuck :-s):
function mesec_svi(){ global $konekcija; $mes=$_get['mes']; $query= "select distinct vol_id handouts mes = {$mes} "; $mesec_svi = mysqli_query($konekcija, $query); potvrda_query($mesec_svi); while($mesec_svi_lista=mysqli_fetch_assoc($mesec_svi)){ $volid= $mesec_svi_lista["vol_id"]; echo $volid.' '; $query1= "select handout_item_id handouts vol_id = {$volid} , mes = {$mes}"; $mesec_svi_o = mysqli_query($konekcija, $query1); potvrda_query($mesec_svi_o); while($mesec_svi_olista=mysqli_fetch_assoc($mesec_svi_o)){ $itemid= $mesec_svi_olista["handout_item_id"]; $query2= "select item_value items itemid = {$itemid}"; $pr_svi_o = mysqli_query($konekcija, $query2); potvrda_query($pr_svi_o); while($pr_svi_olista=mysqli_fetch_assoc($pr_svi_o)){ $pr= $pr_svi_olista["item_value"]; echo '</br>'.$pr.'</br>';} } } }
this displaying results:
1 200 100 50 2 50 50 100
where 1 , 2 volunteer ids , other numbers items value. need values summed.
1 350 2 200
i tried sum() , array_sum no luck.
try inner join
. query -
select sum(item_value) sum_item handouts inner join items on items.itemid = handouts.handout_item_id vol_id = {$volid} , mes = {$mes} group items.itemid
and loop through -
while($mesec_svi_olista=mysqli_fetch_assoc($mesec_svi_o)){ echo $mesec_svi_olista['sum_item'].'<br>'; }
Comments
Post a Comment