简体   繁体   中英

Ordering with Group by Mysql

Additional help anyone.

I'm trying to get the value of per column 1 and 2 based on time which is column 4.

This is my table

在此处输入图像描述

This is what I'm trying to achieve as a result.

在此处输入图像描述

as you can see the last row is not there anymore because row 1 and last row is the same when it comes to sensor_id and event_type but row 1 (time) is higher than last row(time)

This is my code so far

SELECT `sensor_id`,`event_type`,`value`, time FROM `events` ORDER BY `time` DESC

You can use the GROUP BY clause for this purpose:

SELECT `sensor_id`, `event_type`, `value`, max(`time`)
FROM `events`
GROUP BY `sensor_id`, `event_type`, `value`
ORDER BY max(`time`) DESC

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM