Kaedah pertanyaan MySQL untuk rekod terakhir: 1. Lihat jadual dan struktur jadual dalam pangkalan data semasa 2. Isih medan ID dalam susunan terbalik mengikut kenaikan automatik ID dan lihat baris pertama; . Menurut pernyataan sisipan semasa dan gunakan fungsi last_insert_id() untuk melihat rekod terakhir.
Persekitaran pengendalian artikel ini: sistem Windows 7, mysql versi 8.0, komputer Dell G3.
Bagaimana untuk menanyakan rekod terakhir dalam mysql?
MySQL menanyakan rekod terakhir
1
1 Lihat jadual dalam pangkalan data semasa
mysql> show tables; +----------------+ | Tables_in_test | +----------------+ | my_insert1 | | my_insert2 | +----------------+
2 Lihat struktur jadual 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
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)
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));
mysql> select * from my_insert1; +-----------+----------------------------------+ | name | password | +-----------+----------------------------------+ | 黄飞鸿 | *6BB4837EB74329105EE4568DDA7DC67 | | 李小龙 | *6BB4837EB74329105EE4568DDA7DC67 | +-----------+----------------------------------+ mysql> select * from my_insert2; +----+-----------+----------------------------------+ | id | name | password | +----+-----------+----------------------------------+ | 1 | 黄飞鸿 | *6BB4837EB74329105EE4568DDA7DC67 | | 2 | 李小龙 | *6BB4837EB74329105EE4568DDA7DC67 | | 3 | 李连杰 | *6BB4837EB74329105EE4568DDA7DC67 | +----+-----------+----------------------------------+
<🎜. >
2. Lihat baris kedua rekod dalam jadual semasamysql> select count(*) from my_insert1; +----------+ | count(*) | +----------+ | 2 | +----------+ 1 row in set (0.00 sec)
mysql> select * from my_insert1 limit 1,1; +-----------+----------------------------------+ | name | password | +-----------+----------------------------------+ | 李小龙 | *6BB4837EB74329105EE4568DDA7DC67 | +-----------+----------------------------------+ 1 row in set (0.00 sec)
4. Berdasarkan kenaikan automatik ID, isikan medan ID dalam susunan terbalik, dan lihat baris Satu
mysql> select * from my_insert2 where id=(select max(id) from my_insert2); +----+-----------+----------------------------------+ | id | name | password | +----+-----------+----------------------------------+ | 3 | 李连杰 | *6BB4837EB74329105EE4568DDA7DC67 | +----+-----------+----------------------------------+
5. Anda boleh menggunakan fungsi berdasarkan last_insert_id(). pada pernyataan sisipan semasa untuk melihat rekod terakhir
mysql> select * from my_insert2 order by id desc limit 1; +----+-----------+----------------------------------+ | id | name | password | +----+-----------+----------------------------------+ | 3 | 李连杰 | *6BB4837EB74329105EE4568DDA7DC67 | +----+-----------+----------------------------------+
[Cadangan berkaitan:
tutorial video mysqlmysql> 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)
Atas ialah kandungan terperinci Bagaimana untuk menanyakan rekod terakhir dalam mysql. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!