gpt4 book ai didi

mysql - 结果表mysql中两行的总和

转载 作者:行者123 更新时间:2023-11-29 19:03:04 25 4
gpt4 key购买 nike

我想从结果查询中获取两行的总和。我已经尝试过这段代码,但它似乎不起作用。但如果我删除 tot,它会起作用,但不会给出总数。

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
. . .

关于mysql - 结果表mysql中两行的总和,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43712389/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com