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

Source: Internet
Author: User

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_incrementIs 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.

Related Article

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.