您好,欢迎访问一九零五行业门户网

Mysql获取id最大值、表的记录总数等相关问题的方法汇总_MySQL

一、mysql 获取当前字段最大id
sql语句:
select max(id) from yourtable;
二、获取mysql表自增(auto_increment)值
auto_increment是表中的一个属性,只要把表的状态获取到,也就可以获取到那个自增值
sql语句:
show table status like “表名”;
php代码实现
$get_table_status_sql = show table status like '表名';$result = mysql_query($get_table_status_sql);$table_status = mysql_fetch_array($result);echo $table_status['auto_increment']; // 这个就是自增值

select max(id) from testnotnull;
三、获取一个表的记录总数
select count(*) from table;

select count(id) from table;
select sql_calc_found_rows * from table_name;select found_rows();
myisam 下count(*)主键 时要 加条件,此条件为 类型 字段,索引无效
不加条件下非常快,加了后慢了两个数量级
使用 show table status 语句是最高效的方法
格式
show table status [{from | in} db_name] [like 'pattern' | where expr]
示例:
show table status from cpdlt like 'lehecai_1202';
总结
以上就是为大家整理的如何获取一个表的记录数、获取一个表的最大id以及获取一个表的auto_increment值等相关问题的全部内容,希望对大家的学习或者工作带来一定的帮助,如果有疑问的大家可以留言交流。
其它类似信息

推荐信息