GROUP BY WITH ROLLUPを使用してテーブルを作成し、nullではなく合計行を取得します。年(日付)と月(日付)にMYSQL GROUP BY WITH ROLLUPを使用する場合、Nullを '合計'に変更できません
$sql ="SELECT
IF(YEAR(transaktioner.datum) is null or YEAR(transaktioner.datum) = '','Total',YEAR(transaktioner.datum)) as Year,
IF(MONTH(transaktioner.datum) is null or MONTH(transaktioner.datum) = '','Total',MONTH(transaktioner.datum)) as Month,
SUM(transaktioner.belopp)*0.01 as Belopp
FROM transaktioner
GROUP BY
Year,
Month
WITH ROLLUP
";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "||Year:". $row["Year"]."|MONTH:" . $row["Month"]. "|Sum:" . $row["Belopp"]. " ";
}
} else {
echo "0 results";
}
$conn->close();
ただし出力は、それが私を与える代わりに、私の真または偽を与えるものではありません:
|Year:2016|MONTH:1|Sum:408.1797 |
|Year:2016|MONTH:10|Sum:-70.6915 |
|Year:2016|MONTH:11|Sum:-189.0771 |
|Year:2016|MONTH:12|Sum:-51.7362 |
|Year:2016|MONTH:2|Sum:67.5001 |
|Year:2016|MONTH:3|Sum:132.6177 |
|Year:2016|MONTH:4|Sum:-36.4121 |
|Year:2016|MONTH:5|Sum:-121.8800 |
|Year:2016|MONTH:6|Sum:294.5811 |
|Year:2016|MONTH:7|Sum:76.3505 |
|Year:2016|MONTH:8|Sum:-201.3231 |
|Year:2016|MONTH:9|Sum:-62.8723 |
|Year:2016|MONTH: |Sum:245.2368 |
|Year:2017|MONTH:1|Sum:156.2617 |
|Year:2017|MONTH:2|Sum:-166.7156 |
|Year:2017|MONTH:3|Sum:-183.3601 |
|Year:2017|MONTH:4|Sum:-213.6732 |
|Year:2017|MONTH:5|Sum:-149.0897 |
|Year:2017|MONTH:6|Sum:-120.2097 |
|Year:2017|MONTH:7|Sum:-302.1064 |
|Year:2017|MONTH:8|Sum:-183.0638 |
|Year:2017|MONTH:9|Sum:-119.3371 |
|Year:2017|MONTH: |Sum:-1281.2939 |
|Year: |MONTH: |Sum:347.6165
それは代わりに「合計」を言うの空白です。何が間違っているのですか?私はifnull()も同様の問題で試しました。
私は、次のフォーマットにそれを持ってしたいと思います:|年:2017 | MONTH '総' |合計:-1281.2939 | – John