2011-06-10 36 views
0
' machine_id time_schedule product_quantity 
'  1       100 
'  1       200   
'  1       100   << find this value(100) 
'  2   05:00:00 
'  2   10:00:00 
'  2   15:00:00      << find this value(15:00:00) 
'  3       100   
'  3       300   << find this value(300) 
'  4       200   
'  4       100   
'  4       50   << find this value(50) 

如何在vb.net中查找mysql數據庫中每個machine_id的最後一行?由於如何在vb.net中查找mysql數據庫中每個machine_id的最後一行?

Private Sub btnAddSchedule_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAddSchedule.Click 
    Try 
     Dim ts As TimeSpan = TimeSpan.Parse(Me.dtpSchedule.Text 

     ' PROBLEM IN HERE ??? 
     Dim rwFound As DataRow = MyDataset.Tables("totalizer_setting").Rows.Find(???) 
     If IsNothing(rwFound) Then 
     Dim rw As DataRow = MyDataset.Tables("totalizer_setting").NewRow 
     rw("machine_id") = Me.txtMachineID.Text 
     rw("time_schedule") = ts 
     rw("product_quantity") = DBNull.Value 
     MyDataset.Tables("totalizer_setting").Rows.Add(rw) 

     Dim sql As String = String.Format("INSERT INTO {0}.totalizer_setting (machine_id, time_schedule, product_quantity) VALUES ({1});", varSchema, "@machine_id, @time_schedule, @product_quantity") 
     Dim cmd As New MySqlCommand(sql, objCon) 
     cmd.Parameters.AddWithValue("@machine_id", Me.txtMachineID.Text) 
     cmd.Parameters.AddWithValue("@time_schedule", ts) 
     cmd.Parameters.AddWithValue("@product_quantity", DBNull.Value) 
     MyDataAdapter.InsertCommand = cmd 
     MyDataAdapter.Update(MyDataset, "totalizer_setting") 
     cmd.Dispose() 

     End If 
    Catch ex As MySqlException 
     MessageBox.Show("Error occurred in btnAddSchedule_Click Function" & vbCrLf & ex.Number & " – " & ex.Message) 
    End Try 
End Sub 
+0

看到答案請參閱http://stackoverflow.com/questions/1379565/mysql-first-and-last-record分組記錄聚合函數,以獲得更高效的解決方案 – eyaler 2012-06-25 12:49:01

回答

2

像這樣的東西應該工作

select last(time_schedule), last(product_quality) from yourTable group by machine_id 

退一步講,我不是當MySQL last()功能(SQL Server會)。如果mysql沒有它,你可以使用first()以相反的順序或從this post

相關問題