4

是否可以在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

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

4

5 回答 5

5

我试过这种方式,多重加入开始和限制。您根据选择删除加入。

    $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();
于 2017-02-17T12:13:42.293 回答
4

这是 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;
   }
}

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

于 2013-02-23T03:27:03.930 回答
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();
}
于 2013-02-27T02:09:00.733 回答
2

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');

field1从 ( tablename)中选择

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

// SELECT field1, field2FROM ( tablename) 限制 10

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

//field2从 ( tablename)中选择

于 2016-03-24T10:11:00.820 回答
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);

链接到文档

于 2013-02-23T03:21:57.943 回答