php - Codeigniter - 如何使用前一个查询的条件进行第二个 MySQL 查询

标签 php mysql codeigniter

嘿,我有以下问题:

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

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

最佳答案

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

如果您想使用第一个查询的结果运行第二个查询 - 那么这不会有帮助

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();
}

关于php - Codeigniter - 如何使用前一个查询的条件进行第二个 MySQL 查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11118732/

相关文章:

mysql - mysql 文件导入的终端命令

php - NULL 上的第三次连接不返回值(Codeigniter)

php - 在 PHP 中使用 AES-128 加密文件并在 Android 中解密

php - 可捕获的 fatal error : Object of class Proxies\__CG__\AppBundle\Entity\Ticket could not be converted to string

mysql - 同步不同数据库的 2 个表 - MySQL

php - Codeigniter 加入两个表显示类别名称并列出所有项目

php - 获取第二个表的计数以及 id

php - 如何组合两个 MySQL 查询,其中一个查询在另一个 while 循环中定义?

PHP PDO Postgres 数据库启动/停止问题

php - 纽约和伦敦之间的时区