嘿,我有以下问题:
我有一个很长的 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/