[英]MySQL PHP Dynamic table
I have a MySQL Data table and I want to get out the datas sum by month and sum by wood type.我有一个 MySQL 数据表,我想按月和木材类型得出数据总和。 I get the total sum but I dont get for wood types.
我得到了总和,但我没有得到木材类型。
Hear is my MySQL table:听听我的 MySQL 表:
furesz:弗雷斯:
wood![]() |
cubic![]() |
date![]() |
machine![]() |
---|---|---|---|
A-001 ![]() |
0,25 ![]() |
2022-02-01 18:13:12 ![]() |
mebor1 ![]() |
A-003 ![]() |
0,35 ![]() |
2022-02-01 18:13:12 ![]() |
mebor1 ![]() |
My code:我的代码:
<div class="col-sm">
<table class="table">
<thead>
<tr>
<th scope="col">Hónap</th>
<th scope="col">Nyár</th>
<th scope="col">Tölgy</th>
<th scope="col">VTölgy</th>
<th scope="col">Cser</th>
<th scope="col">SUM</th>
</tr>
</thead>
<tbody>
<?php
$sql_list =
"SELECT MONTH(date) AS month,
COUNT(DISTINCT DATE(date)) AS work_days,
SUM(IF(cubic='A-001',1,0)) sum_nyar,
SUM(IF(cubic='A-003',1,0)) sum_tölgy,
SUM(IF(cubic='A-004',1,0)) sum_vtölgy,
SUM(IF(cubic='A-018',1,0)) AS sum_cser,
SUM(cubic) AS sum_full
FROM furesz
WHERE machine='mebor2'
GROUP BY month";
$result_list = mysqli_query($conn, $sql_list);
while($row = mysqli_fetch_assoc($result_list)) {
?>
<tr>
<th scope="row"><?=$row['month']?></th>
<td><?=number_format ($row['sum_nyar'],1)?> m3</td>
<td><?=number_format ($row['sum_tölgy'],1)?> m3</td>
<td><?=number_format ($row['sum_vtölgy'],1)?> m3</td>
<td><?=number_format ($row['sum_cser'],1)?> m3</td>
<th><?=number_format ($row['sum_full'],1)?> m3</th>
<?php
}
?>
</tbody>
</table>
</div>
This is the output I get:这是我得到的 output:
month![]() |
nyár![]() |
tölgy![]() |
VTölgy ![]() |
Cser![]() |
SUM![]() |
---|---|---|---|---|---|
2 ![]() |
0.0 m3 ![]() |
0.0 m3 ![]() |
0.0 m3 ![]() |
0.0 m3 ![]() |
0.6 m3 ![]() |
This is what I'am looking for:这就是我正在寻找的:
Month![]() |
Nyár![]() |
Tölgy![]() |
VTölgy ![]() |
Cser![]() |
SUM![]() |
---|---|---|---|---|---|
2 ![]() |
0.25 m3 ![]() |
0.35 m3 ![]() |
0.0 m3 ![]() |
0.0 m3 ![]() |
0.6 m3 ![]() |
The following code would work.以下代码将起作用。 You mixed up the wood and cubic column.
你把木头和立方柱混在一起了。 So you want to sum the cubic if wood equals a string.
所以如果木头等于一根弦,你想对立方求和。
SELECT
MONTH(date) AS month,
COUNT(DISTINCT DATE(date)) AS work_days,
SUM(IF(wood='A-001',cubic,0)) sum_nyar,
SUM(IF(wood='A-003',cubic,0)) sum_tölgy,
SUM(IF(wood='A-004',cubic,0)) sum_vtölgy,
SUM(IF(wood='A-018',cubic,0)) AS sum_cser,
SUM(cubic) AS sum_full
FROM furesz
WHERE machine='mebor2'
GROUP BY month
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.