2017-05-22 1 views
0

COUNTSUMを使用して合計を取得しようとしていますが、NULL値ではなく他の値を取得できました。カントが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'; 
?> 

は、UPDATE、私のコードです:

私が編集コードを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列を。

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'; 

注:

答えて

3

あなたはこれを簡素化することができますあなたが望む

  • オペレータは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; 
+0

おかげで、私は今、正しいテーブルを得ました。しかし、まだNULL値がありません。スクリーンショットで私のアップデートを見てください。 – testyummy

+0

ありがとうございました...これは既に解決されています... NoUpdateが表示されないような日付です – testyummy

関連する問題