2

我有两个表(下面的代码),想从表中检索表的最后一行和每个表的(连接)信息trackers_dataGROUP BY tracker_idtracker_idDevices

设备/表:

        /*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
4

1 回答 1

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
于 2012-07-29T10:21:08.890 回答