2015-06-19 45 views
0

我想知道如何使用LIMITSELECT FOUND_ROWS()包含到现有查询中,或者如果有更好的方法可以获得总行数而不需要LIMITmysqli - 使用SELECT FOUND_ROWS()和预处理语句

//SELECT * just for question 

$q = $this->db->mysqli->prepare("SELECT SQL_CALC_FOUND_ROWS * 
           FROM countries_ship c 
           JOIN items i 
           ON c.item_id = i.id 
           JOIN item_expire e 
           ON c.item_id = e.item_id 
           JOIN users u 
           ON i.user_id = u.id 
           LEFT JOIN bids b 
           ON i.id = b.item_id 
           LEFT JOIN publishers p 
           ON i.item_publisher = p.id 
           LEFT JOIN tags_rel tr 
           ON c.item_id = tr.item_id 
           JOIN tags t 
           ON t.id = tr.tag_id 
           LEFT JOIN countries co 
           ON i.item_location = co.id 
           WHERE ".$where." 
      GROUP BY i.id ORDER BY ".$order." ".$limit.""); 

    $count_rows = $this->db->mysqli->query("SELECT FOUND_ROWS()"); 

/* return of var_dump($count_rows); 
object(mysqli_result)#74 (5) { 
    ["current_field"]=> 
    int(0) 
    ["field_count"]=> 
    int(1) 
    ["lengths"]=> 
    NULL 
    ["num_rows"]=> 
    int(1) 
    ["type"]=> 
    int(0) 
} 
*/ 

$prep = join("", $prep); 

    call_user_func_array('mysqli_stmt_bind_param', array_merge (array($q, $prep), $this->helperClass->valRef($ref))); 

    $q->execute(); 
    $rows = $this->helperClass->bindResults($q); 

    $q->close(); 

    return $rows; 

不知道这是否100%有效,但这是我如何解决我的问题。

  1. 改变了查询开始SELECT SQL_CALC_FOUND_ROWS
  2. 创建新的阵列 - $resArray = array()
  3. 移动的第二查询类似建议,并添加这两种结果$resArray

     $q->execute(); 
    
         $rows = $this->helperClass->bindResults($q); 
    
         $q->close(); 
    
         $count_rows = $this->db->mysqli->query("SELECT FOUND_ROWS()"); 
    
         $count = mysqli_fetch_assoc($count_rows); 
    
         $resArray[] = $count; 
         $resArray[] = $rows; 
    
         return $resArray; 
    
+0

select count(*)as total_item from ... –

+0

不会只是返回带有限制的计数吗? – Ciprian

+1

就像您通常那样:将'SQL_CALC_FOUND_ROWS'添加到您的'SELECT'并发出第二个查询来获得结果。 – jeroen

回答

0

您的SQL_CALC_FOUND_ROWS正处于正确的轨道上,您只需要移动到执行查询以计算结果的位置。因为它只会在运行原始查询后才有效。 因此,将$count_rows = $this->db->mysqli->query("SELECT FOUND_ROWS()");移至$q->execute();之后

+0

'致命错误:未捕获的异常'mysqli_sql_exception'带有消息'Commands out of sync;在'$ q-> execute()'之后移动'$ count_rows'后,你不能立即运行这个命令。所以我把它移到'$ rows = $ this-> helperClass'之后...但'var_dump'结果仍然相同。 – Ciprian

1

虽然我可能是错的,看来,如果你只是想获得那些符合条件的行数,你应该只使用SQL的计数聚合函数是这样的:

SELECT 
    i.id, 
    count(*) 
FROM 
    countries_ship c 
     JOIN items i 
      ON c.item_id = i.id 
     JOIN item_expire e 
      ON c.item_id = e.item_id 
     JOIN users u 
      ON i.user_id = u.id 
     LEFT JOIN bids b 
      ON i.id = b.item_id 
     LEFT JOIN publishers p 
      ON i.item_publisher = p.id 
     LEFT JOIN tags_rel tr 
      ON c.item_id = tr.item_id 
     JOIN tags t 
      ON t.id = tr.tag_id 
     LEFT JOIN countries co 
      ON i.item_location = co.id 
WHERE 
    ".$where." 
GROUP BY 
    i.id 

另一方面,如果你想获得一个计数和所有细节的方式,你可以将它们分页,最好是简单地运行两个查询 - 一个用于聚合计数,另一个用于限制结果。

编辑:我已经在查询中留下了i.id分组,因为它会给你一个计数每i.id匹配的行。如果你只想完全计算TOTAL,你可以将它从select子句和group by中删除,完全摆脱组。

+0

是的...我需要获取详细信息,将行限制为12并计算所有行没有限制。而且我还需要将“COUNT(b.bids)”出价计为出价。这就是为什么我使用'GROUP BY' – Ciprian

相关问题