0

I'm trying to use a MYSQL query to group each month and each year and the total made in that month, i have tried various times but it always seems error out.

My current query:

<table class="table table-condensed table-striped table-hover table-responsive">
    <thead>
    <tr>
        <th>Month</th>
        <th>Year</th>
        <th>Amount</th>     
    </tr>
    </thead>
    <tbody> 
       <?php                
       $p1 = DB::getInstance()->select("SELECT SUM(`pay_cost`) AS `madePerMonth` MONTH(pay_date) as month, YEAR(pay_date) as year FROM `payments` GROUP BY YEAR(pay_date), MONTH(pay_date) DESC");
       ?>
       <?php foreach ($p1 as $r1) { ?>  
        <tr>
            <td><b><?php echo $r1['month']; ?></b></td>
            <td><b><?php echo $r1['year']; ?></b></td>
            <td><b><?php echo $r1['madePerMonth']; ?></b></td>
        </tr>
        <?php } ?>
    </tbody>
</table>

This error shows: 1.An alias was previously found. (near "month" at position 56) can anyone see the issue? any help is appreciated.

colinreedy674
  • 365
  • 2
  • 12

1 Answers1

1

Month or month is a reserved word in MySQL (MySQL Documentation). This could be what is causing your errors. You should use the solution from the comment by Guillermo Andres Fuentes Moral.

Wouter Pol
  • 1,031
  • 1
  • 8
  • 16