我试图从上月总结大量打字(SET)如何计算上个月的数值?
这里的结构:
CREATE TABLE tester (
date_amount date,
amount int);
INSERT INTO tester VALUES
("2014-08-01", 1000),
("2014-08-02", 1001),
("2014-08-03", 1002),
("2014-08-31", 2000),
("2014-08-31", 2000),
("2014-08-31", 2000),
("2014-09-01", 1006);
在例如例如
这里
如果键入年= 2014月= 9,我应该会自动总结在这种情况下键入的所有金额每月的最后一天“2014年8月31日”
这里是demo:
SET @month := 09;
SET @year := 2014;
select sum(amount) from tester
where month(date_amount)= @month-1
查询必须总结前一个月的最后一天的所有款项类型
我必须为结果是:
SUM(AMOUNT)
6000
我试过,但不是正确的方式,因为在这里我知道该月的最后一天:
SET @month := 09;
SET @year := 2014;
select sum(amount) from tester
where month(date_amount)= @month-1 and day(date_amount)= 31
我也试过此查询,但得到NULL
SET @month := 09;
SET @year := 2014;
SELECT sum(amount)FROM tester
WHERE month(date_amount)= @month-1 AND day(date_amount) =last_day(day(date_amount))
请有人可以帮助我呢?
很高兴能帮到你! :D – 2014-10-16 18:03:04
@ThanatosSama我刚刚注意到你几乎有正确的答案。如果你将where子句的第二个条件改为day(last_day(date_amount)),那么在第二次尝试中你得到了'null'。你得到空的原因是因为last_day返回的日期不会等于'day'返回的日期(整数)。 – 2014-10-16 18:16:47
哦,对不起,我没有注意到你写了@日期我不想只输入日期和月份。 – 2014-10-17 14:05:24