我有两个表格(代码如下),想要检索trackers_data
表的最后行以及GROUP BY tracker_id
和(加入)的信息表tracker_id
Devices
设备/表
/*Table: devices*/
id tracker_id name device_name
-----------------------------------
1 1242 Driver A Benz
2 1255 Driver B Volvo
3 1256 Driver C BMW
Trackers_data /表
/*Table: trackers_data*/
id tracker_id longitude latitude
-------------------------------------
5 1255 53.25 11.52
6 1255 52.63 11.63
7 1242 52.23 13.11
8 1242 52.25 15.27
9 1242 54.63 13.86
示例/结果应如下所示:
/* Results */
id tracker_id lonitude latitude name device_name
6 1255 52.63 11.63 Driver B Volvo
9 1242 54.63 13.86 Driver C BMW
答案 0 :(得分:4)
您可以使用此解决方案:
SELECT b.*, c.name, c.device_name
FROM (SELECT MAX(id) AS id FROM trackers_data GROUP BY tracker_id) a
JOIN trackers_data b ON a.id = b.id
JOIN devices c ON b.tracker_id = c.tracker_id