0

ねえ、私は次の問題を抱えています:

非常に長いMySQLクエリがあり、それが返す行をカウントしたいと思います。

この結果を取得した後、制限を付けて再度作成し、結果を列の1つでグループ化します。

したがって、次のようになります。

/* 
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. 

しかし、2番目のクエリは機能しません。どうすればよいですか?

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

私はあなたの質問に少し混乱しています。まったく同じクエリを単純に実行したいが、場合によっては 2 つの追加コマンドを使用する場合は、これで実行できます。

最初のクエリの RESULTS を使用して 2 番目のクエリを実行する場合、これは役に立ちません。

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 に答える