mysql 获取自增id的方式

select Auto_increment from information_schema.tables where Table_Schema='database_name' and table_name='table_name'
use mysql;
SET GLOBAL information_schema_stats_expiry=0;
SET @@GLOBAL.information_schema_stats_expiry=0;
SET SESSION information_schema_stats_expiry=0;
SET @@SESSION.information_schema_stats_expiry=0;

优点:能获取到自增id,很好用

缺点:不易记住,sql复杂。mysql8.0及以上版本加了缓存,导致默认不会获取最新的自增id。请修改配置文件,设置information_schema_stats_expiry=0,或者每次查询前执行analyze table。

Scroll to top