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

Source: Internet
Author: User
After checking the manual, we can find that the SQL _CALC_FOUND_ROWS keyword is used to count the total number of results that meet the filtering conditions (not limited by Limit, if you are interested, you can learn the MYSQL Keyword: SQL _CALC_FOUND_ROWS. After checking the manual, you can find that this keyword is used to calculate the results after the filtering conditions are met during query.

After checking the manual, we can find that the SQL _CALC_FOUND_ROWS keyword is used to count the total number of results that meet the filtering conditions (not limited by Limit, if you are interested, you can learn the MYSQL Keyword: SQL _CALC_FOUND_ROWS. After checking the manual, you can find that this keyword is used to calculate the results after the filtering conditions are met during query.

After reading the manual, I found that the SQL _CALC_FOUND_ROWS keyword is used to count the total number of results that meet the filtering conditions (not limited by Limit) during queries. If you are interested, you can learn the following.

Keywords of MYSQL:
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:

The Code is as follows:


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

The Code is as follows:


SELECT found_rows () AS rowcount;


The returned rowcount is 1000;
This reduces the repeated queries of SELECT count (*) AS rowcount, which can save a considerable amount of time.

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

The Code is as follows:


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;
}

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.