Home > Database > Mysql Tutorial > Summary of methods for Mysql to obtain the maximum value of id, total number of records in the table and other related issues_MySQL

Summary of methods for Mysql to obtain the maximum value of id, total number of records in the table and other related issues_MySQL

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-10-09 08:33:41
Original
1520 people have browsed it

1. Mysql gets the maximum id of the current field

SQL statement:

select max(id) from yourtable;
Copy after login

2. Get the auto_increment value of mysql table

Auto_increment is an attribute in the table. As long as you get the status of the table, you can also get the auto-increment value

SQL statement:

show table status like “表名”;
Copy after login

php code implementation

$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']; // 这个就是自增值
Copy after login

or

select max(id) from testnotnull;
Copy after login

3. Get the total number of records in a table

select count(*) from table;
Copy after login

or

select count(id) from table;
Copy after login

SELECT SQL_CALC_FOUND_ROWS * FROM table_name;
SELECT FOUND_ROWS();
Copy after login

myisam When downloading count(*)primary key, you need to add a condition. This condition is a type field and the index is invalid

It is very fast without adding any conditions, but it is two orders of magnitude slower after adding it

Using the SHOW TABLE STATUS statement is the most efficient method

Format

SHOW TABLE STATUS [{FROM | IN} db_name] [LIKE 'pattern' | WHERE expr]
Copy after login

Example:

SHOW TABLE STATUS FROM cpdlt LIKE 'lehecai_1202';
Copy after login

Summary

The above is all the content of related issues such as how to get the number of records of a table, get the maximum id of a table, and get the auto_increment value of a table. I hope it will be helpful to everyone's study or work. If you have If you have any questions, you can leave a message to communicate.

Related labels:
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template