I need to reverse my output and I can't do it by switching the ORDER BY
clause.
This code is getting the last 12 months, but I need to display them in the opposite order. Right now it has October first, but I need it to come last. If I switched to ORDER BY DATE ASC
then I would get the wrong months.
My code is this:
<?php
$result2 = mysqli_query(
$con,
"SELECT SUM(DISTRIBUTED_AMOUNT / EXCHANGE_RATE)
FROM royalties
GROUP BY DATE
ORDER BY DATE DESC
LIMIT 12"
);
while($row2 = mysqli_fetch_array($result2)) {
echo number_format($row2[0], 2, '.', '') . ', ';
}?>
You can just wrap your query in another select and reverse the order by
:
SELECT res
FROM (
SELECT `date`,SUM(DISTRIBUTED_AMOUNT / EXCHANGE_RATE) AS res
FROM royalties
GROUP BY `date`
ORDER BY `date` DESC LIMIT 12
) a
ORDER BY `date` ASC
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With