SELECT * FROM
(
SELECT t2.*, t1.Id as t1id FROM table t1
LEFT JOIN table t2 ON tl.Id = t2.t1id
-- ORDER BY t2.time DESC DESC
HAVING 1 ORDER BY t2.time DESC
) t
GROUP BY t.t1id
这种是正确的 这里需要用having 后再 order by
-- 分组后 后取出每天最大时间一条的最新位置
(
select GROUP_CONCAT(DATE_FORMAT(b.time, '%m-%d %H),b.CityName ORDER BY b.time DESC)
)
as Location,
错误示例: 这种取到t2.*是未排序的 , max(t2.time) 取的是正确的 最大时间 但是要求是取分组后最大时间的那一整条
SELECT t2.*, max(t2.time) FROM table t1
LEFT JOIN table t2 ON tl.Id = t2.t1id
group by t1.id order by t2.time
~~~
left join
-- 为了取分组后时间最大的 一整条 max(xxx)取的是字段 having 10000是因为mysql自动优化 需要加上having限制
(
select * from (
select lastLocCopy.PointDate,
lastLocCopy.VehicleNumber,
concat_ws('', lastLocCopy.ProvinceName, lastLocCopy.CityName) as Location,
oCopy.Id
from VehicleLatestLocation lastLocCopy
left join VehicleLocationOrder locOrderCopy
on locOrderCopy.VehicleLatestLocationId = lastLocCopy.Id and
locOrderCopy.IsDelete = 0
left join `order` oCopy on oCopy.Id = locOrderCopy.OrderId and oCopy.IsDelete = 0
where oCopy.CustomerOrderNo in ('mc210105001')
and lastLocCopy.IsDelete = 0
having 1000
order by lastLocCopy.PointDate desc
) as a group by a.Id
) as loctionCopy on loctionCopy.Id = o.Id
where o.CustomerOrderNo = '2039922357' group by o.Id, o.CustomerOrderNo
~~~