简体   繁体   English

如何获得具有相同条件的值表格2表格

[英]How To get value form 2 table with same condition

I have 2 tables, t_silm and t_revenue, and I want to sum some both tables with the same condition. 我有2个表,t_silm和t_revenue,我想要将两个表的条​​件相加。 This is sample my table: 这是我的表样本:

https://ibb.co/JtCp80w https://ibb.co/JtCp80w

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

I want to get the result: 我想得到结果:

avg = total / jumlah;

condition =  tw =4 and th = 2018;

Please can somebody help me? 请有人帮帮我吗?

 $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++; } 

Try this query 试试这个查询

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

not sure about what you want. 不确定你想要什么。

First you want to sum the column 'total' : 首先,您要对“总计”列进行求和:

SELECT SUM(total) as total1, th, tw FROM t_silm GROUP BY th,tw

Then you want to sum the column jumlah : 然后你要总结列jumlah:

SELECT SUM(jumlah) as total2, th, tw FROM t_silm GROUP BY th,tw

Group this 2 query in 1 query : 将此2个查询分组为1个查询:

SELECT * 
FROM 
(SELECT SUM(total) as total1, th, tw FROM t_silm GROUP BY th,tw ) as T1
INNER JOIN 
 (SELECT SUM(jumlah) as total2, th, tw FROM t_silm GROUP BY th,tw) as T2
      on T2.tw =T1.tw AND T2.th=T1.th 

Now add the condition and the avg : 现在添加条件和平均值:

SELECT T1.th, T1.tw, total1/total2 as avg
FROM
(SELECT SUM(total) as total1, th, tw FROM t_silm GROUP BY th,tw ) as T1
    INNER JOIN 
     (SELECT SUM(jumlah) as total2, th, tw FROM t_silm GROUP BY th,tw) as T2
          on T2.tw =T1.tw AND T2.th=T1.th 
WHERE T1.tw =4 and T1.th = 2018;

you can use this query 你可以使用这个查询

SELECT sum(t_slim.total) as to1, sum(t_revenue.jumlah) as to2, to1/to2 as taverage 
from t_slim t1 INNER JOIN t_revenue t2 on t1.th=t2.th 
and t1.tw=t2.tw WHERE t1.th=2018 and t1.tw=4

this gives you the average as the answer 这给你平均值作为答案

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从同一表格获取价值 - Get value form same table 如何通过相同条件值连接另一个表中的值 - how to join value from another table by same condition value 如何从相同的表单中获取值结果? - How to get value result from the same form? 如何获得具有不同列值和相同外键的记录表格? - How to get record form table has different column value and same foreign key? 通过单击按钮,在该表单下添加了相同的表单,但是我如何获取打印次数的值并将其存储在表中 - by click of the button the same form is added under that form but how can i get the value of the number of times it getting printed &store it in table PHP如何使用条件获取多个表列值 - how php get multiple table column value with Condition 如何在一个 select 选项中获取选定值,并在此基础上,从 MySQL 表中获取数据,以相同形式显示另一个 select 选项 - How to get selected value in one select option and based on that, fetch data from MySQL table to show another select option in the same form 如何同时从表中获取href值和th值 - How to get href value and a th value from a table at the same time 如何获取使条件为 TRUE 的 if 条件的值 - How to get the value of if condition that make the condition TRUE 如何使用JavaScript在表单提交中获取具有相同名称的字段的值 - How to get value of fields with same name on form submit using javascript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM