一、環境和數據准備
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)
