例如,我有一个这样的表:
product | quantity | something ------------------------------- abc | 5 | 2 xzy | 5 | 2 asd | 10 | 2
当我需要所有记录时,我会这样做,并获得3个结果:
select * from tableName
但我想获得第4行,以表示数量(50)和某物(6)。sql查询中是否可能,或者我必须循环执行查询才能在查询执行后获得总计?我想在查询中做到这一点。
(SELECT product, quantity, something FROM tablename) UNION (SELECT "all" AS product, SUM(quantity) AS quantity, SUM(something) AS something FROM tablename)
这是工作查询。它将在结果的最后添加第四行