嘿,我有以下问题:
我有一个很长的MySQL查询,然后我想计算它返回的行。
得到这个结果后,我想在一定限制的情况下再次制作它,并按其中一列对我的结果进行分组。
所以看起来:
/*
query conditions....
query conditions....
query conditions....
query conditions....
query conditions....
query conditions....
query conditions....
*/
//first query
$query = $this->db->get();
$results = $query->result_array();
$this->db->select("FOUND_ROWS() as cnt");
$cnt_array = $this->db->get()->row_array(); //here is my number of rows
//second query and if instruction
if($sth==0) { $this->db->group_by(...);}
$this->db->limit($count, $from);
$query = $this->db->get();
$results = $query->result_array();
$this->db->select("FOUND_ROWS() as cnt");
$tot_cnt = $this->db->get()->row_array(); //now i want to have number of grouped results.
但是第二个查询不起作用,怎么办?
我对你的问题有点困惑。如果您想简单地运行完全相同的查询,但有时使用两个额外的命令 - 那么这将完成。
如果您想使用第一个查询的 RESULTS 运行第二个查询 - 那么这将无济于事
function _generic_query()
{
/*
query conditions....
query conditions....
query conditions....
query conditions....
query conditions....
query conditions....
query conditions....
*/
return;
}
function first_query()
{
$this->_generic_query();
$query = $this->db->get();
$results = $query->result_array();
$this->db->select("FOUND_ROWS() as cnt");
$cnt_array = $this->db->get()->row_array(); //here is my number of rows
}
function second_query()
{
$this->_generic_query();
if($sth==0) { $this->db->group_by(...);}
$this->db->limit($count, $from);
$query = $this->db->get();
$results = $query->result_array();
$this->db->select("FOUND_ROWS() as cnt");
$tot_cnt = $this->db->get()->row_array();
}
你可以像这样编写多个查询
$query = $this->db->get();
$results = $query->result_array();
$sth = $this->db->select("FOUND_ROWS() as cnt");
if($sth){
$qry2 = $this->db->get();
$result2 = $qry2->result_array();
$sth2 = $this->db->select("FOUND_ROWS() as cnt");
}