帮助进行 mysql 查询
我有一个数据库,其中包含我的在线网上商店的所有交易,我正在尝试进行查询以打印一份简单的财务报表。
它将打印在如下表中:
<th>month</th>
<th>number of sales</th>
<th>money in</th>
<th>money out</th>
<th>result</th>
失败的查询: #1111 - 组函数的无效使用
SELECT
month(transaction_date) as month,
count(incoming_amount > '0') as number_of_sales,
sum(incoming_amount / 1.25) as money_in,
sum(outgoing_amount) as money_out,
sum((incoming_amount / 1.25) - sum(outgoing_amount)) as result
FROM myDB WHERE year(timestamp) = '2011' order by id desc");
有人能指出我正确的方向吗?
i have a DB with all transactions of my online webshop, and im trying to make a query to print out a simple financial statement.
it will be printed in a table like this:
<th>month</th>
<th>number of sales</th>
<th>money in</th>
<th>money out</th>
<th>result</th>
the query that fails with: #1111 - Invalid use of group function
SELECT
month(transaction_date) as month,
count(incoming_amount > '0') as number_of_sales,
sum(incoming_amount / 1.25) as money_in,
sum(outgoing_amount) as money_out,
sum((incoming_amount / 1.25) - sum(outgoing_amount)) as result
FROM myDB WHERE year(timestamp) = '2011' order by id desc");
Can anyone point me in the right direction?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
year(timestamp)
不使用 mysql 索引(如果您在时间戳上定义了索引)count(incoming_amount > ' 0')
不正确sum
看起来也不正确year(timestamp)
does not make use on mysql index (if you have define an index on timestamp)count(incoming_amount > '0')
is incorrectsum
does not looks correct too添加group by语句:
Add group by statement:
基于 @ajreal 的答案,您可以通过重用以前计算的值来加速此查询,如下所示:
如果您选择超出年份,请取消注释相关部分。
请注意,您可以将
DESC
修饰符添加到group by
以首先获取最新结果。Building on @ajreal's answer, you can speed this query up by reusing previously calculated values like so:
If you select beyond the year, uncomment the relevant sections.
Note you can add a
DESC
modifier togroup by
to get the most recent results first.