A convenient way to count the total row number of query results in Mysql save Count (*) _mysql

Source: Internet
Author: User
Tags rowcount
MySQL's keywords:
Sql_calc_found_rows
After viewing the manual, it is found that this keyword is useful when querying the total number of results (not limited by Limit) that meet the filter criteria.

For example:
Copy Code code as follows:

SELECT sql_calc_found_rows tid from cdb_threads WHERE fid=14 LIMIT 1, 10;

Let's say there are 1000 conditions, and here we return 10.
Use now
Copy Code code as follows:

SELECT found_rows () as rowcount;

The returned rowcount is 1000;
This saves a significant amount of time by saving duplicate queries for select COUNT (*) as ROWCOUNT.

Here's how to put it in the swim flavor:
Copy Code code as follows:

function Mail_list_sent ($uid, $start) {
Note that there is no comma between the 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 that meet the criteria, 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;
}
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.