Mysql get the maximum ID, the total number of records of the table, and other related issues of the method rollup _mysql

Source: Internet
Author: User

First, MySQL get the current field maximum ID

SQL statement:

Select Max (id) from yourtable;

Second, obtain the MySQL table self-increase (auto_increment) value

Auto_incrementis a property in a table that can be obtained by taking the state of a table to the self-increment

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 self-appreciation

Or

Select Max (id) from Testnotnull;

Three, get the total number of records of a table

Select COUNT (*) from table;

Or

Select COUNT (id) from table;
SELECT sql_calc_found_rows * FROM table_name;
SELECT found_rows ();

myisam The next count(*) primary key is conditional, this condition is a Type field, the index is invalid

No conditions very fast, added after the slow two orders of magnitude

Using SHOW TABLE STATUS statements is the most efficient method

Format

Show TABLE STATUS [{from | in} db_name] [as ' pattern ' | WHERE Expr]

Example:

Show TABLE STATUS from cpdlt like ' lehecai_1202 ';

Summarize

The above is for everyone to organize how to get a table record number, get a table of the largest ID and get a table of auto_increment value, and other related issues of all content, I hope that the study or work to bring some help, if you have questions you can message exchange.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.