0

嘿我有以下问题:

我有一个很长的 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. 

但是第二个查询不起作用,怎么办?

4

2 回答 2

1

您可以像这样编写多个查询

$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");
}
于 2012-06-20T11:49:57.817 回答
1

我对你的问题有点困惑。如果您想简单地运行完全相同的查询,但有时使用两个额外的命令 - 那么就可以了。

如果您想使用第一个查询的结果运行第二个查询 - 那么这将无济于事

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();
}
于 2012-06-20T13:03:43.190 回答