简体   繁体   中英

How to count column with variety values

I have a table that has 3 columns: 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:

 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:

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

How can I solve this?

在您的查询中,我认为您想要这样的事情

SELECT WorkPlace, COUNT(WorkPlace) FROM permohonan GROUP BY WorkPlace

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM