0

我在 sum 中遇到了一些 mysql 问题。我需要一张这样的发票表:

id invoice_num  net_value  date             total_month
_______________________________________________________
1  100          100.00     21.01.2013       500.00
2  101          300.00     22.01.2013       500.00
3  102          100.00     23.01.2013       500.00
4  103          900.00     01.02.2013       900.00
.
.
.

问题是如何计算一个月内所有发票的总和(即上面的一月),并将这个总和显示为一月发票的最后一列中的值。一般来说,我需要按月分组但在每一行中显示的发票值的总和。提前致谢 :)

4

2 回答 2

2

如果你没有 iso 日期,就让它发生

http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_str-to-date

mysql> SELECT STR_TO_DATE('01.5.2013','%d.%m.%Y');
        -> '2013-05-01'

日期为 ISO 时的查询:

SELECT invoices.id, invoices.invoice_num, invoices.net_value, invoices.date, monthly.total_month
FROM invoices
INNER JOIN
  (SELECT month(date) AS MONTH,
          sum(net_value) AS total_month
   FROM invoices group by month(date)) AS monthly ON monthly.MONTH=MONTH(invoices.date)
于 2013-09-27T13:36:30.890 回答
0
select id,invoice_num,net_value,month(date),sum(total_month) as total_month 
from invoices
group by month(date)
于 2013-09-27T13:38:11.253 回答