0

最後の部分で助けが必要です。どうすれば支出の合計を取り、それを収入から差し引いて残りを得ることができますか....

収入 - 支出 = 残り物...あなたはポイントを得る..

私のmysqlテーブルはそれを行うための最良の方法ではないと思いますが、私は新人であり、これが私が思いついたものです...

// summary income

$query_income = "SELECT type, SUM(january), SUM(february), SUM(march), SUM(april),     SUM(may), SUM(june), SUM(juli), SUM(august), SUM(september), SUM(october), SUM(november), SUM(december) FROM data WHERE year = '2012' AND type = '1'";
$results = mysql_query($query_income);

while($row = mysql_fetch_array( $results )) {

   $total_income = $row['SUM(january)'] + $row['SUM(february)'] + $row['SUM(march)'] + $row['SUM(april)'] + $row['SUM(may)'] + $row['SUM(june)'] + $row['SUM(juli)'] + $row['SUM(august)'] + $row['SUM(september)'] + $row['SUM(october)'] + $row['SUM(november)'] + $row['SUM(december)'];

 echo "<tr>";
  echo "<td><b>Income</b></td>";
  echo "<td style='text-align:right'>" . number_format($row['SUM(january)']) ."</td>";
  echo "<td style='text-align:right'>" . number_format($row['SUM(february)']) ."</td>";
  echo "<td style='text-align:right'>" . number_format($row['SUM(march)']) ."</td>";
  echo "<td style='text-align:right'>" . number_format($row['SUM(april)']) ."</td>";
  echo "<td style='text-align:right'>" . number_format($row['SUM(may)']) ."</td>";
  echo "<td style='text-align:right'>" . number_format($row['SUM(june)']) ."</td>";
  echo "<td style='text-align:right'>" . number_format($row['SUM(juli)']) ."</td>";
  echo "<td style='text-align:right'>" . number_format($row['SUM(august)']) ."</td>";
  echo "<td style='text-align:right'>" . number_format($row['SUM(september)']) ."</td>";
  echo "<td style='text-align:right'>" . number_format($row['SUM(october)']) ."</td>";
  echo "<td style='text-align:right'>" . number_format($row['SUM(november)']) ."</td>";
  echo "<td style='text-align:right'>" . number_format($row['SUM(december)']) ."</td>";
  echo "<td style='text-align:right'>" . number_format($total_income) ."</td>";
 echo "</tr>";

}

// summary expenses

$query_expense = "SELECT type, SUM(january), SUM(february), SUM(march), SUM(april), SUM(may), SUM(june), SUM(juli), SUM(august), SUM(september), SUM(october), SUM(november), SUM(december) FROM data WHERE year = '2012' AND type = '2'";
  $results = mysql_query($query_expense);

while($row1 = mysql_fetch_array( $results )) {

   $total_expense = $row1['SUM(january)'] + $row1['SUM(february)'] + $row1['SUM(march)'] + $row1['SUM(april)'] + $row1['SUM(may)'] + $row1['SUM(june)'] + $row1['SUM(juli)'] + $row1['SUM(august)'] + $row1['SUM(september)'] + $row1['SUM(october)'] + $row1['SUM(november)'] + $row1['SUM(december)'];


   echo "<tr>";
  echo "<td><b>Expenses</b></td>";
  echo "<td style='text-align:right'>" . number_format($row1['SUM(january)']) ."</td>";
  echo "<td style='text-align:right'>" . number_format($row1['SUM(february)']) ."</td>";
  echo "<td style='text-align:right'>" . number_format($row1['SUM(march)']) ."</td>";
  echo "<td style='text-align:right'>" . number_format($row1['SUM(april)']) ."</td>";
  echo "<td style='text-align:right'>" . number_format($row1['SUM(may)']) ."</td>";
  echo "<td style='text-align:right'>" . number_format($row1['SUM(june)']) ."</td>";
  echo "<td style='text-align:right'>" . number_format($row1['SUM(juli)']) ."</td>";
  echo "<td style='text-align:right'>" . number_format($row1['SUM(august)']) ."</td>";
  echo "<td style='text-align:right'>" . number_format($row1['SUM(september)']) ."</td>";
  echo "<td style='text-align:right'>" . number_format($row1['SUM(october)']) ."</td>";
  echo "<td style='text-align:right'>" . number_format($row1['SUM(november)']) ."</td>";
  echo "<td style='text-align:right'>" . number_format($row1['SUM(december)']) ."</td>";
  echo "<td style='text-align:right'>" . number_format($total_expense) ."</td>";
 echo "</tr>";

}

// summary, whats left

   echo "<tr>";
  echo "<td><b>Leftover</b></td>";
  echo "<td style='text-align:right'></td>";
  echo "<td style='text-align:right'></td>";
  echo "<td style='text-align:right'></td>";
  echo "<td style='text-align:right'></td>";
  echo "<td style='text-align:right'></td>";
  echo "<td style='text-align:right'></td>";
  echo "<td style='text-align:right'></td>";
  echo "<td style='text-align:right'></td>";
  echo "<td style='text-align:right'></td>";
  echo "<td style='text-align:right'></td>";
  echo "<td style='text-align:right'></td>";
  echo "<td style='text-align:right'></td>";
  echo "<td style='text-align:right'></td>";
 echo "</tr>";
4

2 に答える 2

1

次のような 1 つのクエリでこれを行うことができます。

SELECT type, SUM(january) AS `jan`, SUM(february) AS `feb`, ...
FROM data
WHERE `year` = '2012'
GROUP BY type
ORDER BY type ASC

次に、次のような結果を処理します。

$totals = array();

while($row = mysql_fetch_array( $results )) {
   $type = 'unknown';
   switch ((integer)$row['type']) {
       1:
           $type = 'income';
           break;
       2:
           $type = 'expense';
           break;
   }
   unset($row['type']);
   $totals[$type] = $row;
}

$total_income = array_sum($totals['income']);
$total_expense = array_sum($totals['expenses']);

これで、arr 全体を簡単に合計できる多次元配列ができました。

于 2012-09-18T21:59:54.460 に答える
0

このようなものはどうですか?

SELECT 
    income.1 - expense.1 AS 1,
    income.2 - expense.2 AS 2,
    income.3 - expense.3 AS 3,
    income.4 - expense.4 AS 4,
    income.5 - expense.5 AS 5,
    income.6 - expense.6 AS 6,
    income.7 - expense.7 AS 7,
    income.8 - expense.8 AS 8,
    income.9 - expense.9 AS 9,
    income.10 - expense.10 AS 10,
    income.11 - expense.11 AS 11,
    income.12 - expense.12 AS 12
FROM
    (SELECT
        SUM(january) AS 1, 
        SUM(february) AS 2,  
        SUM(march) AS 3,  
        SUM(april) AS 4,  
        SUM(may) AS 5,  
        SUM(june) AS 6,  
        SUM(juli) AS 7,  
        SUM(august) AS 8,  
        SUM(september) AS 9, 
        SUM(october) AS 10, 
        SUM(november) AS 11, 
        SUM(december) AS 12
    FROM data 
    WHERE year = '2012' AND type = '1') income,
    (SELECT type  AS 1,  
        SUM(january) AS 1, 
        SUM(february) AS 2,  
        SUM(march) AS 3,  
        SUM(april) AS 4,  
        SUM(may) AS 5,  
        SUM(june) AS 6,  
        SUM(juli) AS 7,  
        SUM(august) AS 8,  
        SUM(september) AS 9, 
        SUM(october) AS 10, 
        SUM(november) AS 11, 
        SUM(december) AS 12
    FROM data 
    WHERE year = '2012' AND type = '2') expense
于 2012-09-18T21:59:46.023 に答える