mysql 取最后一条数据 分组

分组查询最新记录的两种方式
##第一种
select
*
from
IVMP_DEVICE d,
IVMP_DEVICE_STATE ds
where
d.DEVICE_SERIES = ds.DEVICE_SERIES
and ds.HAPPEN_TIME = (
select
max(HAPPEN_TIME)
from
IVMP_DEVICE_STATE ids
where
ids.DEVICE_SERIES = ds.DEVICE_SERIES)
group by ds.DEVICE_SERIES

##第二种
找出分组的最新时间,再作为子查询查出分组最新记录,
SELECT
*
FROM
IVMP_DEVICE_STATE AS ds,
(
SELECT
DEVICE_SERIES,
max(HAPPEN_TIME) as HAPPEN_TIME
FROM
IVMP_DEVICE_STATE ids
GROUP BY
DEVICE_SERIES
) AS b
WHERE
ds.DEVICE_SERIES = b.DEVICE_SERIES
AND ds.HAPPEN_TIME = b.HAPPEN_TIME
ORDER BY ds.DEVICE_SERIES

posted @ 2020-03-05 17:11  Struts-pring  阅读(812)  评论(0编辑  收藏  举报