A convenient way to count the total number of rows of query results in MYSQL

Source: Internet
Author: User

MYSQL:

SQL_CALC_FOUND_ROWS 

 

After viewing 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:
[sql] 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
[sql] 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:
[SQL] function mail_list_sent ($ uid, $ start) {// note that no comma $ 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 that meet the conditions in the SELECT statement, unrelated to 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 ;}

 

 

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.