我有以下查询。
SELECT Day(vehicleTrip.startDateTime) As day,
Month(vehicleTrip.startDateTime) As month,
Sum(TIMESTAMPDIFF(SECOND, startDateTime, endDateTime )) As totalDuration,
count(vehicleTrip.startDateTime) As totalTrip,
sum(endMileage-startMileage) As totalMleage
FROM vehicleTrip
WHERE vehicleTrip.vehicleID=:vehicleID
AND vehicleTrip.vehicleEndCodeID!=1
AND vehicleTrip.startDateTime BETWEEN :startDateTime And :endDateTime
Group By Day(vehicleTrip.startDateTime),Month(vehicleTrip.startDateTime)
目前它工作正常,因为它可以按每日日期分组duration, totalTrip, totalMileage etc.
但唯一的问题是我希望日期由 desc 安排。我试过按它给出的 startDateTime desc 下订单me error that its not in GROUP By clause and containts nonaggregated column
。我已经尝试了很多方法,比如把它Max(vehicleTrip.startDateTime)
还是一样的。我怎样才能最好地解决这个问题?
下面是我的表格设计及其索引。
CREATE TABLE `vehicleTrip` (
`vehicleEndCodeID` tinyint NOT NULL DEFAULT '1',
`vehicleID` mediumint NOT NULL,
`startDateTime` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`endDateTime` timestamp NULL DEFAULT NULL,
`startMileage` float(11,3) DEFAULT NULL,
`endMileage` float(11,3) DEFAULT NULL,
`startLatitude` float(10,8) NOT NULL,
`startLongitude` float(11,8) NOT NULL,
`endLatitude` float(10,8) DEFAULT NULL,
`endLongitude` float(11,8) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
ALTER TABLE `vehicleTrip`
ADD PRIMARY KEY (`vehicleID`,`startDateTime`) USING BTREE;
COMMIT;