我总是遇到一些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
.
.
.
问题是如何计算一个月的所有发票的总和(即上面的1月),并将此总和显示为1月份发票的最后一栏中的值。 一般来说,我需要按月分组的发票值的总和,但在每行中显示。 在此先感谢:)
答案 0 :(得分: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)
答案 1 :(得分:0)
select id,invoice_num,net_value,month(date),sum(total_month) as total_month
from invoices
group by month(date)