我有以下MySQL表
CREATE TABLE `mileage` (
`id` int(11) NOT NULL auto_increment,
`dt` date default NULL,
`vehicle` varchar(50) default NULL,
`qty` double default NULL,
`km` bigint(20) default NULL,
`status` varchar(10) default NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=6 ;
INSERT INTO `mileage` (`id`, `dt`, `vehicle`, `qty`, `km`, `status`) VALUES
(1, '2016-03-01', 'CA1234', 100, 2030, 'Full'),
(2, '2016-03-02', 'CA1234', 50, 2300, 'Not-Full'),
(3, '2016-03-02', 'CA1234', 25, 2650, 'Not-Full'),
(4, '2016-03-03', 'CA1234', 70, 3010, 'Not-Full'),
(5, '2016-03-06', 'CA1234', 100, 3550, 'Full'),
(6, '2016-03-07', 'CA1234', 60, 3800, 'Not-Full'),
(7, '2016-03-08', 'CA1234', 40, 4000, 'Not-Full'),
(8, '2016-03-09', 'CA1234', 35, 4200, 'Full'),
(9, '2016-03-11', 'CA1234', 40, 4400, 'Not-Full');
我是MySQL新手。我希望结果看起来像这样。我在网上搜索了很多教程,但我无法得到我想要的结果
Sno Date Vehicle Fuel Qty Kilometer Reading Tank Status Distance Tank Full Distance Tank Full Fuel Qty Mileage
1 2016-03-01 CA1234 100 2030 Full 0 0 0 0
2 2016-03-02 CA1234 50 2300 Not-Full 270 0 0 0
3 2016-03-02 CA1234 25 2650 Not-Full 350 0 0 0
4 2016-03-03 CA1234 70 3010 Not-Full 360 0 0 0
5 2016-03-06 CA1234 100 3550 Full 540 1520 245 6.204081633
6 2016-03-07 CA1234 60 3800 Not-Full 250 0 0 0
7 2016-03-08 CA1234 40 4000 Not-Full 200 0 0 0
8 2016-03-09 CA1234 35 4200 Full 200 650 135 4.814814815
9 2016-03-11 CA1234 40 4400 Not-Full 200 0 0 0
我试过
SELECT @row_num := @row_num +1 AS SNo,X.vehicle as VehicleNo,
X.dt as Date,
X.qty as Quantity,X.status AS TankStatus,
X.km as KilometerReading,if( @lastSN = DATE_FORMAT(X.dt, '%Y-%m-%d'),
X.km - @lastValue, 0000 ) as Distance,
@lastSN := DATE_FORMAT(X.dt, '%Y-%m-%d') AS A,
@lastValue := X.km AS B FROM (
SELECT id ,dt ,vehicle ,qty ,km ,status from mileage
)X ,
( select @lastSN := 0, @lastValue := 0, @row_num :=0 ) SQLVars order by X.dt