如何从Mysql Query获取下一行值

时间:2017-08-09 04:51:30

标签: mysql yii sqlyog

我想使用SQLYog从MySql查询中获取(选择)下一个行值  相同的id_voyage_order?

示例:

id_timesheet | id_voyage_order | Duration
----------------------------------------

1            | 106             | 0.00
2            | 106             | 24.00
3            | 210             | 12.00
4            | 106             | 12.00
5            | 210             | 24.00

我想选择它并在View文件中生成Yii基于相同的id_voyage顺序变为这样并设置最后一条记录的持续时间变为0:

id_timesheet | id_voyage_order | Duration
1.           | 106             | 24.00
2.           | 106             | 12.00
4.           | 106             | 0.00

1 个答案:

答案 0 :(得分:0)

例如:

SELECT a.id_timesheet
     , a.id_voyage_order
     , COALESCE(b.duration,0) duration
  FROM my_table a
  LEFT
  JOIN  
     ( SELECT x.*
             , MAX(y.id_timesheet) previous
          FROM my_table x 
          JOIN my_table y 
            ON y.id_voyage_order = x.id_voyage_order 
           AND y.id_timesheet < x.id_timesheet 
         GROUP 
            BY x.id_timesheet
      ) b
     ON b.previous = a.id_timesheet
  WHERE a.id_voyage_order = 106;