MYSQL中統計查詢成果總行數的便捷辦法省去count(*)。本站提示廣大學習愛好者:(MYSQL中統計查詢成果總行數的便捷辦法省去count(*))文章只能為提供參考,不一定能成為您想要的結果。以下是MYSQL中統計查詢成果總行數的便捷辦法省去count(*)正文
MYSQL的症結詞 :
SQL_CALC_FOUND_ROWS
檢查手冊後發明此症結詞的感化是在查詢時統計知足過濾前提後的成果的總數(不受 Limit 的限制)
例如:
SELECT SQL_CALC_FOUND_ROWS tid FROM cdb_threads WHERE fid=14 LIMIT 1,10;
假定知足前提的有1000條,這裡前往10條。
立刻應用
SELECT found_rows() AS rowcount;
則前往的 rowcount 為1000;
如許節儉了SELECT count(*) AS rowcount的反復查詢,可以節儉比擬可不雅的時光。
以下是該放在在游味中的運用:
function mail_list_sent( $uid, $start ) {
// 留意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 );
//查詢SELECT中知足前提的行數,與LIMIT子句有關
$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;
}