簡體   English   中英

來自結果表mysql的兩行總和

[英]Sum of two rows from result table mysql

我想從結果查詢中獲得兩行的總和。 我已經嘗試過此代碼,但似乎無法正常工作。 但是,如果我刪除小孩,它是可行的,但不會給出總數。

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)

一個問題(至少)是您不能在定義別名的同一SELECT中重用別名。 所以:

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);

我也建議您包括所有要插入的列。 這是一個養成的好習慣。 所以:

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