2

您好,我对汇总 mysql 查询有疑问。我想得到每列的总数。我使用 mysql SUM() 和 ROLLUP 来获得结果。但是在添加的新行上,它会复制最后一行的数据。我怎样才能删除这些数据。我提供了一张关于我真正需要实现的目标的图片。

这是我的查询。

SELECT IFNULL(payments.id, "General Total"), payments.driver_id, payments.vehicle_specifications_id,payments.admins_id, payments.vehicle_id, payments.boundaries, payments.cashbond_payments, payments.loans, payments.penalties, SUM(payments.total_payments), a.first_name, a.mid_name, a.last_name, d2.date, d.first_name, d.mid_name, d.last_name, v.plate_number, v2.car_rate FROM payments INNER JOIN admins AS a ON payments.admins_id = a.id INNER JOIN drivers AS d ON payments.driver_id = d.id INNER JOIN dispatch AS d2 ON payments.dispatch_id =d2.id INNER JOIN vehicles AS v ON payments.vehicle_id = v.id INNER JOIN vehicle_specifications AS v2 ON payments.vehicle_specifications_id = v2.id GROUP BY payments.id WITH ROLLUP

在此处输入图像描述

4

1 回答 1

2

试试这个union all

SELECT 
payments.id, 
payments.driver_id, 
payments.vehicle_specifications_id,
payments.admins_id, 
payments.vehicle_id, 
payments.boundaries, 
payments.cashbond_payments, 
payments.loans, 
payments.penalties, 
SUM(payments.total_payments), 
a.first_name, 
a.mid_name, 
a.last_name, 
d2.date, 
d.first_name, 
d.mid_name, 
d.last_name, 
v.plate_number, 
v2.car_rate 
FROM payments 
INNER JOIN admins AS a ON payments.admins_id = a.id 
INNER JOIN drivers AS d ON payments.driver_id = d.id 
INNER JOIN dispatch AS d2 ON payments.dispatch_id =d2.id 
INNER JOIN vehicles AS v ON payments.vehicle_id = v.id 
INNER JOIN vehicle_specifications AS v2 ON payments.vehicle_specifications_id = v2.id 
GROUP BY payments.id 

union all
select 
'General Total' as id,
'' as driver_id, 
'' as vehicle_specifications_id,
'' as admins_id, 
'' as vehicle_id, 
'' as boundaries, 
'' as cashbond_payments, 
'' as loans, 
'' as penalties, 
SUM(payments.total_payments), 
'' as first_name, 
'' as mid_name, 
'' as last_name, 
'' as date, 
'' as first_name, 
'' as mid_name, 
'' as last_name, 
'' as plate_number, 
'' as car_rate 
FROM payments 
INNER JOIN admins AS a ON payments.admins_id = a.id 
INNER JOIN drivers AS d ON payments.driver_id = d.id 
INNER JOIN dispatch AS d2 ON payments.dispatch_id =d2.id 
INNER JOIN vehicles AS v ON payments.vehicle_id = v.id 
INNER JOIN vehicle_specifications AS v2 ON payments.vehicle_specifications_id = v2.id 
于 2015-05-11T13:06:36.237 回答