[英]select last row in mysql with no-auto incremental
我正在嘗試建立一個臨時日志記錄數據庫。 仍處於學習水平。
如何從數據庫中獲取最后一行?
select * from tempdat order by tdate desc limit 1
給出當天的第一條記錄,但我想要最后一條。 如下所示,它可能會返回輸入21:25:03
時間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)
只需使用兩個條件進行排序:首先是tdate
,然后是ttime
。 例如:
SELECT *
FROM tempdat
ORDER BY tdate DESC, ttime DESC
LIMIT 1;
請注意,應在使用的每一列之后指定順序方向。 ORDER BY tdate, ttime DESC
將首先按tdate ASC
所有記錄進行排序。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.