【问题标题】:how to combine query() and limit() methods in CodeIgniter如何在 CodeIgniter 中结合 query() 和 limit() 方法
【发布时间】:2013-02-08 19:21:17
【问题描述】:

是否可以在codeigniter中做这样的事情:

publi function getcat($category_id)
{
    $query = "(SELECT cat.id, cat.title ";
    $query = $query . "FROM bf_categories cat ";
    $query = $query . "WHERE cat.id=" . $category_id;
    $this->db->limit(2, 4);
    $records = $this->db->query($query);
    return $records->result();
}

我为了演示的目的简化了查询……但它实际上相当复杂,这就是我决定使用query() 方法的原因。

但查询中未包含限制子句...我已通过在控制器中启用 codeigniter 分析器进行验证,我可以看到查询在没有任何限制子句的情况下运行。

您能告诉我如何使用query() 方法完成此操作吗?

编辑 1

我已将模型修改为如下所示:

public function get_categories_and_products($limit=5, $offset=0, $category_id=null)
{
    print "<BR>the function got the following offeset: $offset and limit: $limit";
    $query = "(SELECT cat.category_id, cat.title, cat.image_thumb, cat.deleted, cat.display_weight ";
            $query = $query."FROM bf_categories cat ";
            $query = $query."WHERE cat.parent_id=".$category_id;
    $query = $query." AND cat.category_id <>".$category_id;
    $query = $query.") UNION (";
    $query = $query."SELECT p.product_id, p.name, p.image_thumb, p.deleted , p.display_weight";
    $query = $query." FROM bf_product p ";
    $query = $query."Inner join bf_product_category cp ";
    $query = $query."on p.product_id=cp.product_id ";
    $query = $query."Where cp.category_id=".$category_id.") ?";

            $records = $this->db->query($query,array($this->db->limit(2, 4)));
            return $records->result();
     }

我现在已经硬编码了限制值......但最终,我将使用传递给方法的值。不幸的是,这段代码仍然不起作用。 根据分析器,这是正在执行的内容:

(SELECT cat.category_id, cat.title, cat.image_thumb, cat.deleted, cat.display_weight FROM bf_categories cat WHERE cat.parent_id=3 AND cat.category_id <>3) UNION (SELECT p.product_id, p.name, p.image_thumb, p.deleted , p.display_weight FROM bf_product p Inner join bf_product_category cp on p.product_id=cp.product_id Where cp.category_id=3)

SELECT * FROM (`bf_categories`) WHERE `category_id` = '3' LIMIT 4, 2

所以它创建了两个单独的选择语句。

【问题讨论】:

    标签: php mysql codeigniter activerecord limit


    【解决方案1】:

    你可以使用

    $query = "(SELECT cat.id, cat.title ";
    $query = $query."FROM bf_categories cat ";
    $query = $query."WHERE cat.id=".$category_id." ?";
    
    $this->db->query($query,array($this->db->limit(2, 4)));
    

     $this->db->select('id,title')->from('bf_categories ')->where('id', $id)->limit(2, 4);
    

    link to doc

    【讨论】:

    • 我唯一要补充的是,OP 的示例不能按预期方式工作的原因是它试图不正确地组合 query() 函数,这通常意味着以数据库的本地语言执行 sql 查询,而 limit() 函数是 CodeIgniter Active Record 库的一部分,该库是与数据库无关的查询结构。
    • 实际上他并没有尝试添加限制,他根本没有添加它,他只是使用$this-&gt;db-&gt;limit(2, 4);认为可能会自动添加限制
    • Arun,我刚刚尝试更新我的代码以匹配您的示例,但它不起作用。请查看我帖子中的编辑 1。谢谢!
    • 我想你错过了 符号
    • @dot...您为什么不简单地将您的 LIMIT 子句添加到 SQL...您已经手动编写了 SQL 行的其余部分,那么还有什么字符呢?
    【解决方案2】:

    这是 CI 中查询的样子:

    $this->db->select("id, title");
    $this->db->where("id", $category_id);
    $this->db->from("bf_categories");
    $this->db->limit($limit);
    $query = $this->db->get();
    
    if($query->num_rows() > 0)
    {
       foreach($query->result() as $row)
       {
          echo $row->title;
       }
    }
    

    如果您包含完整的查询,解决问题或向您展示更好、更有效的方法会更容易。

    【讨论】:

    • 这是资源密集型你没有应用限制
    【解决方案3】:

    最后,我都是手动完成的。

    public function get_categories_and_products($limit=10, $offset=0, $category_id=null)
    {
        $query = "(SELECT cat.category_id, cat.title, cat.image_thumb, cat.deleted, cat.display_weight ";
        $query = $query."FROM bf_categories cat ";
        $query = $query."WHERE cat.parent_id=".$category_id;
        $query = $query." AND cat.category_id <>".$category_id;
        $query = $query.") UNION (";
        $query = $query."SELECT p.product_id, p.name, p.image_thumb, p.deleted , p.display_weight";
        $query = $query." FROM bf_product p ";
        $query = $query."Inner join bf_product_category cp ";
        $query = $query."on p.product_id=cp.product_id ";
        $query = $query."Where cp.category_id=".$category_id.") limit ".$offset.','.$limit;
        $catsandprods= $this->db->query($query);
        return $catsandprods->result();
    }
    

    【讨论】:

      【解决方案4】:

      http://code-igniter.ru/user_guide/database/active_record.html

      $this->db->start_cache();
      $this->db->select('field1');
      $this->db->stop_cache();
      $this->db->get('tablename');
      

      从 (tablename) 中选择 field1

      $this->db->select('field2');
      $this->db->limit(10);
      $this->db->get('tablename');
      

      // SELECT field1, field2 FROM (tablename) 限制 10

      $this->db->flush_cache();
      $this->db->select('field2');
      $this->db->get('tablename');
      

      // 选择field2 FROM (tablename)

      【讨论】:

        【解决方案5】:

        我尝试过这种方式,使用 start 和 limit 进行多重连接。您根据选择删除加入。

            $this->db->select('*');
            $this->db->from('tbl_requestservice as r');
            $this->db->join('tbl_service as s','s.service_id=r.service_id');
            $this->db->limit($limit, $start);
            $query = $this->db->get();
            print_r($query->result());
            return $query->result();
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2023-03-23
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2010-11-27
          • 1970-01-01
          相关资源
          最近更新 更多