【问题标题】:use count_all_results from query使用查询中的 count_all_results
【发布时间】:2013-01-29 04:17:26
【问题描述】:

我有复杂的查询,说它是 $complexQuery, 那么我需要从中获取所有数据行号,而不需要数据结果。

我研究过 count_all_results() 比 num_rows() 好

现在说出我的代码:

$complexQuery = 'Some sql query';
$q = $this->db->query($complexQuery);
$total1 = $q->num_rows();

现在我很困惑从该查询中获取所有总数据, 对该查询使用 $this->db->count_all_results() 有何建议?

== 通过编辑 DB_active_rec.php 解决 ==

我这样做(如果表名包含“选择”,则保持原样):

public function from($from)
{
    foreach ((array)$from as $val)
    {
        if (strpos($val, ',') !== FALSE)
        {
            foreach (explode(',', $val) as $v)
            {
                $v = trim($v);
                $this->_track_aliases($v);
                $v = $this->ar_from[] = $this->_protect_identifiers($v, TRUE, NULL, FALSE);

                if ($this->ar_caching === TRUE)
                {
                    $this->ar_cache_from[] = $v;
                    $this->ar_cache_exists[] = 'from';
                }
            }
        }
        else
        {
            $val = trim($val);

            // Added to bypass from arr if $val contained 'select' for complex query
            // $this->db->count_all_rows("select * from tableName")
            // will be select count(1) from (select * from tableName)
            if(FALSE !== strpos(strtolower($val),'select')){
                $this->ar_from[] = "($val)";
            }else{
                // Extract any aliases that might exist. We use this information
                // in the _protect_identifiers to know whether to add a table prefix
                $this->_track_aliases($val);
                $this->ar_from[] = $val = $this->_protect_identifiers($val, TRUE, NULL, FALSE);
            }

            if ($this->ar_caching === TRUE)
            {
                $this->ar_cache_from[] = $val;
                $this->ar_cache_exists[] = 'from';
            }
        }
    }

    return $this;
}

【问题讨论】:

  • 你想解释什么?
  • @user2020329 你是怎么解决的? php文件在哪里?

标签: php codeigniter codeigniter-2


【解决方案1】:

应该是这样的:

$this->db->where($complexQuery);
$this->db->from('your_table_name');
echo $this->db->count_all_results();

见:Codeigniter count_all_results()

【讨论】:

  • 我不能..所有查询都在$complexQuery里面,里面会有任何表和连接表。
【解决方案2】:

再过几分钟,但代码相同

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

      class SomeMyModel extends CI_Model {

      private static $db;

      function __construct() 
      {
        parent::__construct();
        self::$db = &get_instance()->db;
      }

        static function countTableResults(){
          self::$db->where($coplexQuery);
           return self::$db->count_all_results('#TABLENAME#');
        }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-12-21
    • 1970-01-01
    • 1970-01-01
    • 2011-11-25
    • 2023-03-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多