【发布时间】: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)之前好像少了一个逗号 -
谢谢大家,这是保留字和缺少逗号的组合。