先前数据的下一个值

时间:2018-04-09 18:41:54

标签: mysql

我正在尝试创建一个查询,通过查看数据以前的结果来查询下一个结果。

所以我的数据就像

Train - Station   -  Time
158     Station1     11:10
158     Station1     11:11
158     Station1     11:12
158     Station1     11:13
158     Station1     11:14
158     Station2     11:25
158     Station2     11:26
158     Station2     11:27
158     Station3     11:41
158     Station3     11:42
158     Station3     11:43
158     Station3     11:44
158     Station3     11:45
158     Station4     11:50
158     Station4     11:51
158     Station4     11:52
158     Station4     11:53

让我们说“我在”Station3“

我使用以下查询来查找以前的站点是什么:

SELECT * FROM Train 
WHERE Train = '158' AND Station NOT LIKE 'Station3' 
ORDER BY Time DESC 
LIMIT 1

我想查询下一站是什么,但我真的不知道要做出那个查询。有什么提示吗?

修改 好吧,我们可以说我的表有更多的信息,关于以前的列车运行,并且我想检查下一站将是什么。

    火车 - 车站 - 时间
    158 Station1 10:10
    158 Station1 10:11
    158站1 10:12
    158站1 10:13
    158 Station1 10:14
    158站2 10:25
    158站2 10:26
    158站2 10:27
    158 Station3 10:41
    158 Station3 10:42
    158站3 10:43
    158站3 10:44
    158 Station3 10:45
    158 Station4 10:50
    158 Station4 10:51
    158 Station4 10:52
    158 Station4 10:53
    158 Station5 10:55
    158 Station5 10:56
    158站6 10:57
    158站6 10:58
    158 Station1 11:10
    158 Station1 11:11
    158 Station1 11:12
    158 Station1 11:13
    158 Station1 11:14
    158站2 11:25
    158 Station2 11:26
    158 Station2 11:27
    158 Station3 11:41
    158 Station3 11:42
    158 Station3 11:43
    158 Station3 11:44
    158 Station3 11:45
    158 Station4 11:50
    158 Station4 11:51
    158 Station4 11:52
    158 Station4 11:53
    159 Station1 11:10
    159 Station1 11:11
    159 Station1 11:12
    159 Station1 11:13
    159 Station1 11:14
    159 Station2 11:25
    159 Station2 11:26
    159 Station2 11:27
    159 Station3 11:41
    159 Station3 11:42
    159 Station3 11:43
    159 Station3 11:44
    159 Station3 11:45
    159 Station4 11:50
    158 Station4 11:51
    159 Station4 11:52
    159 Station4 11:53



所以,让我说在火车158站4时间11:53,我不知道下一站是什么,我想查询它是Station5。我怎么会这样做?

3 个答案:

答案 0 :(得分:2)

如果您只需要知道下一站,您可以使用子查询获取station3和train 158的最大时间

  select * from 
  my_table 
  where time > ( 
  select max(time) max_time
  from my_table 
  where Train = '158' AND Station = 'Station3' )
  and train = '158'
  order by time limit 1 

答案 1 :(得分:2)

其他方法,首先找到站点的MAX(时间)。

MySQL 8.0+使LEAD能够获得下一个记录数据真的很蠢。

<强>查询

SELECT
   Table1.Train
 , Table1.next_station AS station
 , Table1.next_station_time AS time
FROM ( 

   SELECT 
     *
     , LEAD(Station) OVER (PARTITION BY Train ORDER BY Time) AS next_station
     , LEAD(Time) OVER (PARTITION BY Train ORDER BY Time) AS next_station_time
   FROM
    Table1
   WHERE
     Train = 158 
)
 AS Table1

WHERE
   Table1.station = 'station3'
 AND
   Table1.station <> Table1.next_station

参见演示https://www.db-fiddle.com/f/9Ld7XznMEuzNdRC3dmysUt/1

在较旧的MySQL版本中,我们需要更具创意,因为LEAD不受支持。
模拟LEAD的最佳方法是使用MySQL的用户变量和一个自动LEFT JOIN转换 请记住MySQL用户变量在MySQL 5.7 +

中工作

<强>查询

SELECT 
   current_train AS train
 , next_station AS station
 , next_time AS time
FROM ( 

  SELECT 
      t1.Train AS current_train
    , t1.Station AS current_station
    , t1.Time AS `current_time`
    , t2.Train AS next_train
    , t2.Station AS next_station
    , t2.Time AS next_time
  FROM (
    SELECT 
      *
      , @rownum1 := @rownum1 + 1 AS rownum
    FROM 
      Table1
    CROSS JOIN ( SELECT @rownum1 := 0 ) AS i 
    WHERE
      Train = 158
    ORDER BY 
      Time ASC
  ) AS t1
  LEFT JOIN (
    SELECT 
      *
      , @rownum2 := @rownum2 + 1 AS rownum
    FROM 
      Table1
    CROSS JOIN ( SELECT @rownum2 := 0 ) AS i   
    WHERE
      Train = 158 
    ORDER BY 
      Time ASC 
  ) AS t2
  ON   
   t1.rownum + 1 = t2.rownum
 ) 
 AS records
WHERE
   records.current_station = 'station3'
 AND
   records.current_station <> records.next_station

请参阅演示http://sqlfiddle.com/#!9/ff23fc/38

答案 2 :(得分:0)

以更简单的方式,使用自我join可以帮助您实现它。

试试这个:

select t1.* from train t1 
right join train t2 on t1.train=t2.train
where t1.station != 'Station3' and t1.time>(t2.time)
and t2.station='Station3' limit 1 ;

尝试将表stationt1上的t2值更改为您希望的值,在这种情况下为Station3

Live SQL Fiddle demo here