Mysql查询组最新插入
我有一个表 tbldevicevaluelog
,其布局如下:
id | ts | dc | data1 | data2
其中 ts
是时间戳,dc
是设备代码。 data1
和 data2
是最新值。
总共有 130 种不同的设备代码。
目标是从每个可用的设备代码中获取最新时间和 data1
。
通过下面的查询,我可以获得包含所有设备代码和最新时间的结果。
SELECT dc, MAX(ts) FROM tbldevicevaluelog GROUP BY dc
问题是如何也获取属于这个最新时间的data1
?
I have a table tbldevicevaluelog
with the following layout:
id | ts | dc | data1 | data2
Where ts
is a timestamp, dc
is a devicecode. data1
and data2
are the latest value.
In total there are 130 different device codes.
The goal is to get the latest time and data1
from each available device code.
With the query below I can get a result with al the device codes and the latest time.
SELECT dc, MAX(ts) FROM tbldevicevaluelog GROUP BY dc
The question is how to also get the data1
which belongs to this latest time?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用相同的设备代码和时间戳将结果连接回表中。
Join your result back to your table on the same device code and timestamp.