Methods for Mysql to obtain the maximum id value, the total number of table records, and other related issues are summarized. The maximum value of mysql
1. obtain the maximum id of the current field from mysql.
SQL statement:
select max(id) from yourtable;
2. Obtain the Auto_increment value of the mysql table.
Auto_increment
Is an attribute in a table. As long as the table status is obtained, you can get the auto-increment value.
SQL statement:
Show table status like "table name ";
Php code implementation
$ Get_table_status_ SQL = "show table status like 'table name'"; $ result = mysql_query ($ get_table_status_ SQL); $ table_status = mysql_fetch_array ($ result ); echo $ table_status ['Auto _ increment ']; // This is the Auto-increment value.
Or
select max(id) from testnotnull;
3. Obtain the total number of records in a table
select count(*) from table;
Or
select count(id) from table;
SELECT SQL_CALC_FOUND_ROWS * FROM table_name;SELECT FOUND_ROWS();
myisam
Lowercount(*)
A condition must be added when the primary key is used. This condition is a type field and the index is invalid.
It is very fast without adding a value. After adding a value, it is two orders of magnitude slower.
Use SHOW TABLE STATUS
Statement is the most efficient method
Format
SHOW TABLE STATUS [{FROM | IN} db_name] [LIKE 'pattern' | WHERE expr]
Example:
SHOW TABLE STATUS FROM cpdlt LIKE 'lehecai_1202';
Summary
The preceding section describes how to obtain the number of records of a table, obtain the maximum id of a table, and obtain the auto_increment value of a table, I hope to help you in your study or work. If you have any questions, you can leave a message.