t_silmとt_revenueの2つのテーブルがあり、同じ条件で両方のテーブルを合計したいと思います。これは私のテーブルのサンプルです:
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 SUM(total) as total1, th, tw FROM t_silm GROUP BY th,tw
次に、列jumlahを合計します。
SELECT SUM(jumlah) as total2, th, tw FROM t_silm GROUP BY th,tw
この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
次に、条件と平均を追加します。
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;
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加