簡體   English   中英

我如何才能從PHP mysql中的兩個或多個表中求和字段值

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

我有六個表,每個表都有兩個名為(餐,成本)的列。 我可以合計一桌飯。 但是我想對每張桌子的總和求和。

但是像這樣。我有六個桌子。 我想一次匯總每張桌子的費用。

$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>";

有什么辦法嗎?

您可以使用union all來構建具有相同列均值的唯一表,成本形成六個表中的每個表

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 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM