2014-03-29 50 views
1

我試圖做一個臨時日誌數據庫。仍然在學習層面上。在mysql中選擇最後一行,不帶自動增量

如何從數據庫中獲取最後一行?

select * from tempdat order by tdate desc limit 1 

給出了當天的第一個入口,但我想要最後一個。下面看到的,它可以返回入口絲毫時間21:25:03

+------------+----------+------+-------------+ 
| 2014-03-29 | 21:20:02 | inne |  22.875 | 
| 2014-03-29 | 21:25:03 | inne |  22.875 | 
+------------+----------+------+-------------+ 
1933 rows in set (0.16 sec) 

mysql> select * from tempdat order by tdate desc limit 1; 

+------------+----------+------+-------------+ 
| tdate  | ttime | zone | temperature | 
+------------+----------+------+-------------+ 
| 2014-03-29 | 00:00:03 | inne |  21.250 | 
+------------+----------+------+-------------+ 
1 row in set (0.03 sec) 

回答

0

只需使用兩個標準進行訂購:第一tdate,然後ttime。例如:

SELECT * 
     FROM tempdat 
    ORDER BY tdate DESC, ttime DESC 
    LIMIT 1; 

請注意,應在每列使用後指定順序方向。 ORDER BY tdate, ttime DESC將首先訂購所有記錄tdate ASC