无法获得总的NULL值并且无法正确配置表

时间:2017-05-22 11:44:36

标签: php html sql

我想在查询中使用COUNTSUM来获取总计,我能够获取其他值,但不能获取NULL值。

如果您注意到,NoUpdate在屏幕截图中没有任何值。但我的表中肯定有NULL值。

此外,我似乎无法修复结果表,因为我希望我的表看起来像下面, Correct Table

这是我的代码,

<?php
require 'include/DB_Open.php';

$date = $_POST['date'];
$date1 = $_POST['date1'];

$sql ="SELECT 
    COUNT(IF(status='Successful', 1, NULL)) as `Successful`
    , COUNT(IF(status='Failed', 1, NULL)) as `Failed`
    , COUNT(IF(status='Canceled', 1, NULL)) as `Canceled`
    , COUNT(IF(status='RolledBack', 1, NULL)) as `RolledBack`
    , SUM(IF(status='IS NULL', 1, NULL)) as `NoUpdate`
    , COUNT(status) as `Total`
    FROM table
    WHERE date_implemented BETWEEN '$date' AND '$date1'
    GROUP BY Status";

$myData = mysql_query($sql)or die(mysql_error());

$output = 
"<tr>
<th colspan='5' align='center' style='border:dotted 1px; border-top:dotted 1px;' bgcolor='#66CC00'>Status</th>
<th width='auto' align='center' rowspan='2' style='border:dotted 1px; border-top:dotted 1px;' bgcolor='#66CC00'>Total</th>
</tr>
<tr>
    <th width='auto' align='center' style='border:dotted 1px;' bgcolor='#66CC00'>Successful</th>
    <th width='auto' align='center' style='border:dotted 1px;' bgcolor='#66CC00'>Failed</th>
    <th width='auto' align='center' style='border:dotted 1px;' bgcolor='#66CC00'>Canceled</th>
    <th width='auto' align='center' style='border:dotted 1px;' bgcolor='#66CC00'>RolledBack</th>
    <th width='auto' align='center' style='border:dotted 1px;' bgcolor='#66CC00'>NoUpdate</th>
</tr>\n";

$totSuccessful = $totFailed = $totCanceled = $totRolledBack = $totNoUpdate = $totAll = 0;
while (list($successful, $failed, $canceled, $rolledback, $noupdate, $total) = mysql_fetch_row($myData)) {

    $output .= "
        <td align='center' style='border-bottom:dotted 1px;'>$successful</td>
        <td align='center' style='border-bottom:dotted 1px;'>$failed</td>
        <td align='center' style='border-bottom:dotted 1px;'>$canceled</td>
        <td align='center' style='border-bottom:dotted 1px;'>$rolledback</td>
        <td align='center' style='border-bottom:dotted 1px;'>$noupdate</td>
        </tr>\n";
    $totSuccessful += $successful;
    $totFailed += $failed;
    $totCanceled += $canceled;
    $totRolledBack += $rolledback;
    $totNoUpdate += $noupdate;
    $totAll += $total;   
}
$output .= "<tr></th>
<td align='center' style='border-bottom:dotted 1px;' bgcolor='#66CC00'>$totSuccessful</td>
<td align='center' style='border-bottom:dotted 1px;' bgcolor='#66CC00'>$totFailed</td>
<td align='center' style='border-bottom:dotted 1px;' bgcolor='#66CC00'>$totCanceled</td>
<td align='center' style='border-bottom:dotted 1px;' bgcolor='#66CC00'>$totRolledBack</td>
<td align='center' style='border-bottom:dotted 1px;' bgcolor='#66CC00'>$totNoUpdate</td>
<td align='center' style='border-bottom:dotted 1px;' bgcolor='#66CC00'>$totAll</td></tr>\n";

include 'include/DB_Close.php';
?>

更新:

我根据Gordon Linoff的建议编辑代码,现在我能够获得正确的表格。

SELECT SUM(status = 'Successful') as `Successful`,
       SUM(status = 'Failed') as `Failed`,
       SUM(status = 'Canceled') as `Canceled`,
       SUM(status = 'RolledBack') as `RolledBack`,
       SUM(status IS NULL) as `NoUpdate`,
       COUNT(*) as `Total`
FROM table
WHERE date_implemented BETWEEN '$date' AND '$date1';

Fixed Table

但仍然缺少NULL - NoUpdate列的值。

1 个答案:

答案 0 :(得分:3)

您可以将其简化为:

SELECT SUM(status = 'Successful') as `Successful`,
       SUM(status = 'Failed') as `Failed`,
       SUM(status = 'Canceled') as `Canceled`,
       SUM(status = 'RolledBack') as `RolledBack`,
       SUM(status IS NULL) as `NoUpdate`,
       COUNT(*) as `Total`
FROM table
WHERE date_implemented BETWEEN '$date' AND '$date1';

注意:

  • 您想要的运算符为IS NULL而不是= 'IS NULL'。后者是字符串比较。
  • 我简化了逻辑,利用了MySQL将布尔表达式视为运算符的事实。
  • 我删除了GROUP BY
  • 我将最后一个表达式从COUNT(*)更改为COUNT(status)COUNT(status)仅计算非NULL值。 。 。并且名称Total表明您想要一切。

您还可以将每个状态放在一个单独的行中:

SELECT status, COUNT(*) as cnt
FROM table
WHERE date_implemented BETWEEN '$date' AND '$date1'
GROUP BY status;