如何在多个表

时间:2015-04-29 11:44:25

标签: php mysql

我有以下查询执行没有错误,但它没有返回正确的行数。从一个表中选择时,同样的查询有效,但不是两个:

SELECT FOUND_ROWS() as `total` 
FROM `products` t1 
RIGHT JOIN `homepage` t2 ON `t2`.id = `t1`.id;

1 个答案:

答案 0 :(得分:0)

执行第一次

选择SQL_CALC_FOUND_ROWS为total FROM products t1 RIGHT JOIN homepage t2 ON t2。id = t1。id;

然后执行

`SELECT FOUND_ROWS();` // this will give total no. of rows found in above query. 

查询1您可以获取数据并使用它们,从查询2中您可以获得查询1的结果总数

例如

$sql = "SELECT SQL_CALC_FOUND_ROWS from abcd limit 20"; 
$query = $this->db->query($sql); 
$result = $query->result_array(); 

$total = 0;
 if(!empty($result)) { 
$sql2 = "SELECT FOUND_ROWS() as total"; 
$query1 = $this->db->query($sql2); 
$row = $query1->row(); 
$total = $row->total; // suppose you get total as 150 
}