The convenient way to count the total number of rows in the query result in MYSQL saves count (*) _ MySQL

Source: Internet
Author: User
The convenient way to count the total number of rows in the query result in MYSQL saves the keyword count (*) bitsCN. comMYSQL:
SQL _CALC_FOUND_ROWS
After checking the manual, we found that this keyword is used to count the total number of results that meet the filtering conditions during the query (not limited by Limit)

For example:

SELECT SQL _CALC_FOUND_ROWS tid FROM cdb_threads WHERE fid = 14 LIMIT 1, 10;

Suppose there are 1000 conditions, and 10 are returned here.
Use Now

SELECT found_rows () AS rowcount;

The returned rowcount is 1000;
This saves a considerable amount of time for repeated queries of SELECT count (*) AS rowcount.

The following are the applications that should be applied in the game flavor:

Function mail_list_sent ($ uid, $ start ){
// Note that there is no comma between SQL _CALC_FOUND_ROWS uid
$ Query = "SELECT SQL _CALC_FOUND_ROWS uid, real_name, current_city, msg_uid, sender_flag ,".
"Msg_title, msg_content FROM". TT_DBTABLEPRE. "mailbox as mb1,". TT_DBTABLEPRE.
"User as usr1 WHERE mb1.sender _ id = usr1.uid AND mb1.sender _ id = $ uid AND sender_flag> 0 LIMIT $ start,". TT_PAGESIZE;
$ Mails = $ this-> db-> fetch_all ($ query );
// Query the number of rows in the SELECT statement that meet the conditions, regardless of the LIMIT clause.
$ Max_count = $ this-> db-> fetch_first ("SELECT found_rows () AS rowcount ");
$ Tmp ['state _ Code'] = 200;
$ Tmp ['info'] = "OK ";
$ Tmp ['list'] = $ mails;
$ Data = json_encode ($ tmp );
Return $ data;
}
BitsCN.com

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.