查询分组月,年,金额

时间:2017-07-31 21:33:04

标签: php mysql

我正在尝试使用MYSQL查询对每个月和每年进行分组以及该月的总数,我已经尝试了多次,但似乎总是出错。

我当前的查询:

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

此错误显示: 1.之前找到了别名。 (在第56位的“月”附近)任何人都可以看到这个问题吗?任何帮助表示赞赏。

1 个答案:

答案 0 :(得分:1)

月份或月份是MySQL中的保留字(MySQL Documentation)。这可能是导致错误的原因。你应该使用Guillermo Andres Fuentes Moral的评论中的解决方案。

相关问题