mysql查詢最後一筆記錄的方法:1、查看目前資料庫中的表格及表格結構;2、根據ID自增長,對ID欄位進行倒序排序並查看第一行;3、根據目前insert語句並使用函數last_insert_id()查看最後一筆記錄即可。
本文操作環境:windows7系統、mysql8.0版、Dell G3電腦。
mysql怎麼查詢最後一筆?
MySQL查詢最後一筆記錄
一、環境和資料準備
1.查看目前資料庫中的表格
mysql> show tables; +----------------+ | Tables_in_test | +----------------+ | my_insert1 | | my_insert2 | +----------------+
2.查看my_insert1表格結構
mysql> show create table my_insert1\G; *************************** 1. row *************************** Table: my_insert1 Create Table: CREATE TABLE `my_insert1` ( `name` varchar(10) CHARACTER SET latin1 DEFAULT NULL, `password` varchar(32) CHARACTER SET latin1 DEFAULT NULL ) ENGINE=InnoDB DEFAULT CHARSET=utf8 1 row in set (0.00 sec)
3.查看my_insert2表格結構
mysql> show create table my_insert2\G; *************************** 1. row *************************** Table: my_insert2 Create Table: CREATE TABLE `my_insert2` ( `id` int(10) unsigned NOT NULL AUTO_INCREMENT, `name` varchar(10) CHARACTER SET latin1 DEFAULT NULL, `password` varchar(32) CHARACTER SET latin1 DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 1 row in set (0.00 sec)
4.向表my_insert1和my_insert2表中插入記錄
mysql> insert into my_insert1(name,password) values ('黄飞鸿',password(123456)),('李小龙',password(123456)); mysql> insert into my_insert2(id,name,password) values (null,'黄飞鸿',password(123456)),(null,'李小龙',password(123456));
5.查看表的記錄
mysql> select * from my_insert1; +-----------+----------------------------------+ | name | password | +-----------+----------------------------------+ | 黄飞鸿 | *6BB4837EB74329105EE4568DDA7DC67 | | 李小龙 | *6BB4837EB74329105EE4568DDA7DC67 | +-----------+----------------------------------+ mysql> select * from my_insert2; +----+-----------+----------------------------------+ | id | name | password | +----+-----------+----------------------------------+ | 1 | 黄飞鸿 | *6BB4837EB74329105EE4568DDA7DC67 | | 2 | 李小龙 | *6BB4837EB74329105EE4568DDA7DC67 | | 3 | 李连杰 | *6BB4837EB74329105EE4568DDA7DC67 | +----+-----------+----------------------------------+
二、當表中沒有ID自增長字段和有ID自增長查看最後一條記錄的方式
1.由於my_insert1,沒有ID自增長,查看當前表中有多少筆記錄
mysql> select count(*) from my_insert1; +----------+ | count(*) | +----------+ | 2 | +----------+ 1 row in set (0.00 sec)
2.查看當前表的第2行記錄
mysql> select * from my_insert1 limit 1,1; +-----------+----------------------------------+ | name | password | +-----------+----------------------------------+ | 李小龙 | *6BB4837EB74329105EE4568DDA7DC67 | +-----------+----------------------------------+ 1 row in set (0.00 sec)
3.根據ID自增長,使用子查詢查看表ID字段最大值
mysql> select * from my_insert2 where id=(select max(id) from my_insert2); +----+-----------+----------------------------------+ | id | name | password | +----+-----------+----------------------------------+ | 3 | 李连杰 | *6BB4837EB74329105EE4568DDA7DC67 | +----+-----------+----------------------------------+
4.根據ID自增長,對ID字段進行倒序排序,並查看第一行
mysql> select * from my_insert2 order by id desc limit 1; +----+-----------+----------------------------------+ | id | name | password | +----+-----------+----------------------------------+ | 3 | 李连杰 | *6BB4837EB74329105EE4568DDA7DC67 | +----+-----------+----------------------------------+
5.可以根據當前insert語句使用函數last_insert_id(),查看最後一筆記錄
mysql> insert into my_insert2(id,name,password) values(null,'霍元甲',password('123456')); Query OK, 1 row affected, 1 warning (0.00 sec) mysql> select * from my_insert2 where id=(select last_insert_id()); +----+-----------+----------------------------------+ | id | name | password | +----+-----------+----------------------------------+ | 4 | 霍元甲 | *6BB4837EB74329105EE4568DDA7DC67 | +----+-----------+----------------------------------+ 1 row in set (0.00 sec)
【相關推薦:mysql影片教學】
以上是mysql怎麼查詢最後一筆記錄的詳細內容。更多資訊請關注PHP中文網其他相關文章!