0

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?

3
  • $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>"; i use this code Commented Jul 29, 2019 at 5:37
  • Why do you have them as separate tables? If they have the exact same data, can't they be in 1 table? If they must be separate tables, you can sum them separately, union the results together in a subquery and sum that. Commented Jul 29, 2019 at 5:38
  • Check the answer given below +1, but the need to union six separate tables might be a symptom of poor table design, and that you shouldn't have this data spread across six separate tables. Commented Jul 29, 2019 at 5:45

1 Answer 1

1

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 
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.