小编给大家分享一下mysql如何查询最后一条记录,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!
mysql查询最后一条记录的方法:1、查看当前数据库中的表及表结构;2、根据ID自增长,对ID字段进行倒序排序并查看第一行;3、根据当前insert语句并使用函数last_insert_id()查看最后一条记录即可。
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如何查询最后一条记录”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注亿速云行业资讯频道!