Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/59.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql 选择第二高的时间戳和相关列_Mysql_Correlated Subquery - Fatal编程技术网

Mysql 选择第二高的时间戳和相关列

Mysql 选择第二高的时间戳和相关列,mysql,correlated-subquery,Mysql,Correlated Subquery,在包含行李的表中,每个行李都有一个位置列和时间列,我可以通过此查询获取最近的位置: SELECT b.* FROM bag_position b WHERE time = ( SELECT MAX(c.time) FROM bag_position c WHERE c.bag_id = b.bag_id ); 我怎样才能到达第二高的位置?我试过这个,但不起作用: SELECT b.* from bag_position b where time = ( se

在包含行李的表中,每个行李都有一个位置列和时间列,我可以通过此查询获取最近的位置:

SELECT b.* 
FROM bag_position b 
WHERE time = ( 
    SELECT MAX(c.time) 
    FROM bag_position c 
    WHERE c.bag_id = b.bag_id 
);
我怎样才能到达第二高的位置?我试过这个,但不起作用:

SELECT b.*
from bag_position b
where time = ( select max(c.time) from bag_position c where c.time < (select max d.time from bag_position d) and c.bag_id = b.bag_id );
可以使用偏移和限制而不是最大值:


偏移量1限值1应为限值1偏移量1或限值1,1@RaymondNijland . . . 令人沮丧的是,不同的数据库支持偏移。然后语法就不同了。谢谢@GordonLinoff。
select b.*
from bag_position b
where b.time = (select b2.time
                from bag_position b2
                where b2.bag_id = b.bag_id
                order by time desc
                limit 1 offset 1
               );