简体   繁体   English

来自结果表mysql的两行总和

[英]Sum of two rows from result table mysql

I want to get sum of two rows from the result query. 我想从结果查询中获得两行的总和。 I have tried this code but it doesn't seem to work. 我已经尝试过此代码,但似乎无法正常工作。 But if i remove tot it worked but doesn't give total. 但是,如果我删除小孩,它是可行的,但不会给出总数。

INSERT INTO totaltransportcost
    select year(date) as y, month(date) as m, sum(FuelCost) as fc, SUM(OtherExpenses) as oe, SUM(fc+as) as tot
    from turn
    group by year(date), month(date)

One problem (at least) is that you cannot re-use aliases in the same SELECT where they are defined. 一个问题(至少)是您不能在定义别名的同一SELECT中重用别名。 So: 所以:

INSERT INTO totaltransportcost
    select year(date) as y, month(date) as m, sum(FuelCost) as fc, 
           SUM(OtherExpenses) as oe,
           SUM(FuelCost + OtherExpenses) as tot
    from turn
    group by year(date), month(date);

I would also to advise you to include all the columns being inserted. 我也建议您包括所有要插入的列。 This is a good habit to get into. 这是一个养成的好习惯。 So: 所以:

insert into totaltransportcost(y, m, fc, oe, tot)  -- or whatever the column names are
     . . .

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM