php - 如何在相同条件下从 2 表中获取值
问题描述
我有 2 个表,t_silm 和 t_revenue,我想对具有相同条件的两个表求和。这是我的表示例:
t_silm
bl - th - tw - total
10 - 2018 - 4 - 100
11 - 2018 - 4 - 200
12 - 2018 - 4 - 300
t_revenue
bl - th - tw - jumlah
10 - 2018 - 4 - 25
11 - 2018 - 4 - 70
12 - 2018 - 4 - 45
我想得到结果:
avg = total / jumlah;
condition = tw =4 and th = 2018;
请问有人可以帮我吗?
$query = "SELECT sum(t_slim.pemakaian_309) as to1, sum(t_revenue.jumlah) as to2, to1/to2 as taverage
from t_slim t1 INNER JOIN t_revenue t2 on t1.tahun=t2.tahun
and t1.triwulan=t2.triwulan WHERE t1.tahun=$tahun and t1.triwulan=$triwulan GROUP BY bulan";
$hasil = mysql_query($query);
$baris = 3;
while ($data = mysql_fetch_array($hasil))
{
$worksheet2->write_string(11, $baris, number_format($data['taverage'],2),$format2);
$baris++;
}
解决方案
试试这个查询
SELECT t1.total as total1, t2.total as total2, total1/total2 as total_avg
from t_slim t1 INNER JOIN t_revenue t2 on t1.th=t2.th
and t2.tw=t1.tw WHERE t1.th=2018 and t1.tw=4
推荐阅读
- git - 检测/防止对 git 中的文件进行不必要的更改
- c# - 在设置程序例程后,将属性绑定到 Datagrid 上的文本框不显示值
- c# - .Net Core MVC 多个计划的 Quartz 作业和单例范围
- java - EAR 编译时出现 java 11 部署错误
- sql - 将第 1 行的值除以第 2 行的值并将其放在第 3 行
- python - pandas.errors.EmptyDataError,但文件不为空
- javascript - 从 beforeEach 中提取函数并分配给 `this` 变量
- azure - Azure 应用程序网关受源 IP 和服务器名称限制
- javascript - 根据 Google Apps 脚本函数的返回值以 HTML 格式显示单选按钮
- sql - SQL Server:在导出期间将点更改为逗号