mysql怎么分组取时间最大的那条_Mysql 取分组后时间最大的一整条数据 及一些分组后取出每天最大时间一条...

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

~~~

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值