Sheikh Hasan
Sheikh Hasan

Reputation: 1

how i can SUM field values from two or more tables in php mysql

I have six tables with each has two columns named (meal, cost). I can SUM meal of one table. but I want to SUM the total meal of every table.

but like this.i have six tables. and I want to SUM the cost of every table at a time.

$qq="select SUM(cost5) as 'sumcost' from shawon"; 
$res=mysqli_query($conn,$qq); $data=mysqli_fetch_array($res);  
echo "<div class='container'>". "sum of cost: ".$data['sumcost']."</div>";

is there any way to do it?

Upvotes: 0

Views: 40

Answers (1)

ScaisEdge
ScaisEdge

Reputation: 133360

You can use union all for buil an unique table with the same column mean, cost form each of six tables

select meal, sum(cost)
from (
  select  meal,cost
  from table1 
  union all 
  select  meal,cost
  from table2
  union all 
  select  meal,cost
  from table3
  union all 
  select  meal,cost
  from table4
  union all 
  select  meal,cost
  from table5
  union all 
  select  meal,cost
  from table6
  ) t 
  group by  meal 

Upvotes: 1

Related Questions