【问题标题】:Query to group month,year, amount查询分组月、年、金额
【发布时间】:2017-07-31 21:33:04
【问题描述】:

我正在尝试使用 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 的“月”) 任何人都可以看到这个问题吗?任何帮助表示赞赏。

【问题讨论】:

  • 更改别名MONTH(pay_date) as month(月)和YEAR(pay_date) as year(年)示例更改MONTH(pay_date) as month_alias
  • 您在MONTH(pay_date) 之前好像少了一个逗号
  • 谢谢大家,这是保留字和缺少逗号的组合。

标签: php mysql


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-03-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-26
    • 1970-01-01
    相关资源
    最近更新 更多