当前位置: 移动技术网 > IT编程>数据库>Mysql > Mysql获取id最大值、表的记录总数等相关问题的方法汇总

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

2017年12月12日  | 移动技术网IT编程  | 我要评论

一、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值等相关问题的全部内容,希望对大家的学习或者工作带来一定的帮助,如果有疑问的大家可以留言交流。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网