Adding total values from multiple table in MySQL with php -


i have 2 tables, need sum total value each tables price column. did research on stack solutions saw long , did not seems efficient enough code. mysql trying total value storecoins plus total value storememorabilia , total value supplies want sum of total of both table's column. first code not working...

if($results = $this->dbconn->query("select sum(column) inventoryvalue (                                         select sum(column) value storecoins                                         union                                         select sum(column) value storememorabilia                                         ) allposts")){ while($data = $results->fetch_assoc()){     $totalval = $data['inventoryvalue'];     $totalval .= ".00"; 

i need know doing wrong in code above.

on note (this php related): have following script once correct value returned decides put commas dollar values. recommendation more efficient way create function , results. switch statement works fine , need do, want more eloquent way of writing this.

switch(strlen($val)){     case 7:         $rem = substr($val, 1, 6);         $val = substr_replace($val, ",", 1);         $val = $val.$rem;         break;     case 8:         $rem = substr($val, 2, 6);         $val = substr_replace($val, ",", 2);         $val = $val.$rem;         break;     case 9:         $rem = substr($val, 3, 6);         $val = substr_replace($val, ",", 3);         $val = $totalval.$rem;         break;     case 10:         $rem = substr($val, 1, 10);         $val = substr_replace($val, ",", 1);         $val = $val.$rem;         $rema = substr($val, 5, 6);         $val = substr_replace($val, ",", 5);         $val = $val.$rema; } 

i hope following after:

select (     ( select sum(`price`) `storecoins` )          +      ( select sum(`price`) `storememorabilia` ) ) 'total'; 

Comments

Popular posts from this blog

java - Date formats difference between yyyy-MM-dd'T'HH:mm:ss and yyyy-MM-dd'T'HH:mm:ssXXX -

c# - Get rid of xmlns attribute when adding node to existing xml -