2017-08-09 63 views
0

我想使用SQLYog從MySql查詢中獲取(選擇)下一行值,與 相同的id_voyage_order?如何從Mysql查詢下一行值查詢

例子:

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 

我要選擇它,並在查看文件的Yii基於同一id_voyage順序變成了這個樣子,並設置了最後一條記錄的時間變爲0:

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

在這個網站上,你會嘗試查詢你自己的第一個,然後尋求幫助,如果灰泥ķ。例如,下面的查詢可以做你需要的嗎? *選擇id_timesheet,id_voyage_order,持續時間來自that_table order by duration DESC,其中id_voyage_order = 106 * nb:提供表名稱總是有幫助的。 –

+0

TY @Used_By_Already :) SELECT id_timesheet,id_voyage_order,持續時間從 時間表 WHERE id_voyage_order = '106' 那麼如何讓病情得到下一行的值? –

+0

我是sory,我修復了id @Strawberry –

回答

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; 
+0

非常感謝你。它工作很好。 –