如何选择每辆出租车的最新位置
我想在下面的sql脚本中存储出租车位置,我尝试选择每辆出租车的最新位置,如果出租车标识中的一个存在多个位置数据,则查询应采用最新位置
CREATE TABLE Taxi_Loc (
LocIds INT(255) AUTO_INCREMENT,
TaxiId INT(10) NOT NULL,
Latitude FLOAT(10,6) ,
Longitude FLOAT(10,6),
CreateDate TIMESTAMP,
PRIMARY KEY (LocIds),
FOREIGN KEY (TaxiId) REFERENCES Taxis(TaxiId)
)
我的桌子是
LocId TaxiId Latitude Longitude CreatedDate
2 2 -33.729752 150.836090 12/26/18 01:46 AM
3 1 -33.812222 151.143707 12/26/18 01:46 AM
4 1 -33.903557 151.237732 12/26/18 01:46 AM
5 3 -33.949448 151.008591 12/26/18 01:46 AM
6 2 -33.815521 151.026642 12/26/18 01:46 AM
7 4 -33.796669 151.183609 12/26/18 01:46 AM
8 3 -33.829525 150.873764 12/26/18 01:46 AM
9 5 -33.944489 150.854706 12/26/18 01:46 AM
10 4 -33.873882 151.177460 12/26/18 01:46 AM
11 1 -33.737885 151.235260 12/26/18 04:26 PM
我想得到这个结果
LocId TaxiId Latitude Longitude CreatedDate
2 2 -33.729752 150.836090 12/26/18 01:46 AM
3 1 -33.812222 151.143707 12/26/18 01:46 AM
5 3 -33.949448 151.008591 12/26/18 01:46 AM
7 4 -33.796669 151.183609 12/26/18 01:46 AM
9 5 -33.944489 150.854706 12/26/18 01:46 AM
答案 0 :(得分:2)
SELECT * FROM Taxi_Loc WHERE LocIds IN (SELECT MAX(LocIds) FROM Taxi_Loc GROUP BY TaxiId)
说明:
以下子查询有助于查找相关的LocIds
。假设最新的LocIds
具有更高的数字。
通过Taxi_Loc GROUP BY TaxiId选择MAX(LocIds)