我不知道我是否正确描述了标题中的问题,但是在这里。在MySQL数据库中获取特定字段为最大的id列表
我有一个MySQL数据库中的以下表:
路线点:
routepoint_id | route_id | latitude | longitude | location | routepoint_time
_________________|__________|________________|___________________|__________|__________________
24 | 38 | 9.32454363 | 10.65785453 | blah | 1232455678
25 | 38 | 56.72112300 | 11.40735700 | blah | 3324546454363
26 | 39 | 12.83276562 | 12.83276562 | blah | 54321325541
27 | 39 | 52.72112300 | 10.23400000 | blah | 64321325541
_________________|__________|________________|___________________|__________|__________________
| | | | |
和路线
route_id | user_id | creation_date
_______________|___________|________________
38 | 18 | 1243256576
39 | 16 | 3463468576543
_______________|___________|________________
| |
我所试图做的是这样的: 从选择数据路由点表,其中route_point_time具有最大值的user_id的给定列表。
我试图通过PHP和mysqli的,我给下面的查询USER_ID阵列来实现这一目标:
$stmt = $this->con->prepare("SELECT routes.user_id, routepoints.latitude, routepoints.longitude, MAX(routepoints.routepoint_time) AS maxTime, users.status
FROM routepoints
INNER JOIN routes ON routepoints.route_id = routes.route_id
INNER JOIN users ON routes.user_id = users.user_id
WHERE routes.user_id IN ({$idArray})
GROUP BY routes.user_id");
$stmt->execute();
$stmt->store_result();
$stmt->bind_result($user_id, $latitude, $longitude, $routePointTime, $status);
上面的查询返回以下结果:
{
"status": true,
"friendsLocationList": [
{
"userId": 16,
"latitude": 12.83276562,
"longitude": 12.83276562,
"routePointTime": "64321325541",
"onlineStatus": 0
},
{
"userId": 18,
"latitude": 9.32454363,
"longitude": 10.65785453,
"routePointTime": "3324546454363",
"onlineStatus": 0
}
]
}
,你可以看,问题是它返回routepoint_time的最大值,但它不匹配关联值的其余部分(纬度,经度等)。
我该如何做到这一点?
我希望你明白我想达到的目标。任何帮助将不胜感激。
这个问题被问及经常回答,它只是无聊 – Strawberry
@Strawberry,指向重复,让我们关闭问题。 – innoSPG
@innoSPG建立一个更好的规范问题系统,我会 – Strawberry