Thursday 15 May 2014

sql - Selecting multiple "most recent by timestamp" in mysql -


I have log entries for different servers, for each idServer .

  mysql> To create a scene with the latest entry log entry. Description of server log; + ---------- + ----------- + ------ + ----- + ------------- ------ + ---------------- + | Field | Type | Faucet Key | Default | Extra | + ---------- + ----------- + ------ + ----- + ------------- ------ + ---------------- + | Idlog | Int (11) | No | PRI | Faucet Auto_interpretation | | IDServer | Int (11) | No | Child | Faucet | | Time | Timestamp | No | | CURRENT_TIMESTAMP | | | Text | Text | No | | Faucet | + ---------- + ----------- + ------ + ----- + ------------- ------ + ---------------- + mysql & gt; * Select * from Serverlog; + ------- + ---------- + --------------------- + -------- ---- + | Idlog | IDServer | Time | Text | + ------- + ---------- + --------------------- + -------- ---- + | 1 | 1 | 2009-12-01 15:50:27 | Log line 2 | | 2 | 1 | 2009-12-01 15:50:32 | Log line 1 | | 3 | 3 | 2009-12-01 15:51:43 | Log line 3 | | 4 | 1 | 2009-12-01 10:20:30 | Log line 0 + ------- + ---------- + --------------------- + -------- ---- +  

What's hard about this (for me):

  • Entries for the previous date / time can be inserted later, therefore I do not trust idlog only.
  • The timestamps are not unique, so I need to use idLog as the "latest" tiebraker.

I can get the results I want to use a subquery, but I can not put a subquery in one view. Additionally, I have heard that the subquery display in Myquq is useless .

  mysql> SELECT * FROM (Select * Server DOSC, IDLog Server Service Order by DESC ID Group by ID Group; + ------- + ---------- + --------------------- + -------- ---- + | Idlog | IDServer | Time | Text | + ------- + ---------- + --------------------- + -------- ---- + | 2 | 1 | 2009-12-01 15:50:32 | Log line 1 | | 3 | 3 | 2009-12-01 15:51:43 | Log line 3 | + ------- + ---------- + --------------------- + -------- ---- +  

What is the right way to write my thoughts?

I recommend using:

  Or change as VW_your_view. * To connect to the server (Select Slediser, Max (SL.time) from 'max_time', SERVERLOG SL Group by SL.Disverver) x on x.iderver = t.idersver and x.max_time = t.time  

Never define ORDER BY in a scene because there is no guarantee that whenever you use the view, then the order you specified is required .


No comments:

Post a Comment