[英]How to count column with variety values
I have a table that has 3 columns: EmployeeID, WorkPlace, WorkStatus 我有一个包含3列的表:EmployeeID,WorkPlace,WorkStatus
EmployeeID | WorkPlace | WorkStatus
1 | KL | Baru
2 | KK | Batal
3 | PP | Ubah
4 | KL | Batal
I want to make a report from data in that table and display in a PHP page in table form like this: 我想从该表中的数据生成报告,并以如下表形式在PHP页面中显示:
No. | WorkPlace | WorkStatus | Total |
| | Baru | Batal | Ubah | |
-------------------------------------------------------
1 | KL | 1 | 1 | 0 | 2 |
2 | KK | 0 | 1 | 0 | 1 |
3 | PP | 0 | 0 | 1 | 1 |
-------------------------------------------------------
Total | 1 | 2 | 1 | 4 |
This is my SQL query: 这是我的SQL查询:
mysql_select_db($database_conn, $conn);
$query = "SELECT * FROM permohonan GROUP BY WorkPlace";
$Recordset1 = mysql_query($query, $conn) or die(mysql_error());
while this is my code to display the table: 这是我显示表的代码:
<table border="1" cellpadding="5" style="border-collapse:collapse">
<tr>
<th rowspan="2">No.</th>
<th rowspan="2">WorkPlacen</th>
<th colspan="3">WorkStatus</th>
<th rowspan="2">Total</th>
</tr>
<tr>
<th>baru</th>
<th>batal</th>
<th>ubah</th>
</tr>
<?php
$bil==0; $sBaru=0; $sBatal=0; $sUbah=0;
while ($row_Recordset1 = mysql_fetch_assoc($Recordset1)) {
$bil++; $cBaru=0; $cBatal=0; $cUbah=0;
if ($row_Recordset1['WorkStatus'] == 'Baru'){
$cBaru++;
$sBaru++; }
else if ($row_Recordset1['WorkStatus'] == 'Batal'){
$cBatal++; $sBatal++;}
else if($row_Recordset1['WorkStatus'] == 'Ubah'){
$cUbah++; $sUbah++;}?>
<tr>
<td><?php echo $bil."." ?></td>
<td><?php echo $row_Recordset1['bhg_cwgn']; ?></td>
<td><?php echo $cBaru ?></td>
<td><?php echo $cBatal ?></td>
<td><?php echo $cUbah ?></td>
<td><?php echo $cBaru+$cBatal+$cUbah ?></td>
</tr>
<?php } ?>
<tr>
<td colspan=2>Jumlah</td>
<td><?php echo $sBaru ?></td>
<td><?php echo $sBatal ?></td>
<td><?php echo $sUbah ?></td>
<td><?php echo $sBaru+$sBatal+$sUbah ?></td>
</tr>
</table>
When I use that code, the display for when row WorkPlace = KL for WorkStatus is 1,0,0 instead of 1,1,0 当我使用该代码时,WorkStatus行WorkPlace = KL时的显示为1,0,0,而不是1,1,0
How can I solve this? 我该如何解决?
在您的查询中,我认为您想要这样的事情
SELECT WorkPlace, COUNT(WorkPlace) FROM permohonan GROUP BY WorkPlace
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.