0
我有一個具有3列的表:僱員,工作場所,WorkStatus如何計算與各種列的值
EmployeeID | WorkPlace | WorkStatus
1 | KL | Baru
2 | KK | Batal
3 | PP | Ubah
4 | KL | Batal
我想在表中一個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 |
這是我的SQL查詢:
mysql_select_db($database_conn, $conn);
$query = "SELECT * FROM permohonan GROUP BY WorkPlace";
$Recordset1 = mysql_query($query, $conn) or die(mysql_error());
,而這是我的代碼顯示該表:
<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>
當我使用該代碼,顯示了當行的WorkPlace = KL爲WorkStatus是1,0,0而不是1,1,0
我該如何解決這個問題?
擺脫GROUP BY的。並停止使用PHP的DEPRECATED MYSQL API – Strawberry