【问题标题】:How to write this query in codeigniter active records如何在 codeigniter 活动记录中编写此查询
【发布时间】:2015-09-27 06:56:37
【问题描述】:

如何在 codeigniter 活动记录中编写此查询

        select 
        a.parent_cat_id,a.parent_cat_name, b.child_cat_id,b.child_cat_name,
        c.sub_child_cat_id,c.sub_child_cat_name
        FROM parent_categories a,child_categories b,sub_child_categories c 
        WHERE a.parent_cat_id=b.parent_cat_id AND b.child_cat_id=c.child_cat_id

试过了,结果显示为 0

        $this->db->select('a.parent_cat_id,a.parent_cat_name, b.child_cat_id,b.child_cat_name,c.sub_child_cat_id,c.sub_child_cat_name');
        $this->db->from('parent_categories a,child_categories b,sub_child_categories c');
        $this->db->where('a.parent_cat_id','b.parent_cat_id'); 
        $this->db->where('b.child_cat_id','c.child_cat_id'); 
        $result = $this->db->get()->result_array();

当我回显上面的 ci 查询时,我得到了

SELECT `a`.`parent_cat_id`, `a`.`parent_cat_name`, `b`.`child_cat_id`, `b`.`child_cat_name`, `c`.`sub_child_cat_id`, `c`.`sub_child_cat_name`
FROM `parent_categories` `a`, `child_categories` `b`, `sub_child_categories` `c`
WHERE `a`.`parent_cat_id` = 'b.parent_cat_id'
AND `b`.`child_cat_id` = 'c.child_cat_id' 

【问题讨论】:

    标签: php codeigniter


    【解决方案1】:

    尝试在您的查询中更改$this->db->where,如下所示-

    $this->db->select('a.parent_cat_id,a.parent_cat_name, b.child_cat_id,b.child_cat_name,c.sub_child_cat_id,c.sub_child_cat_name');
        $this->db->from('parent_categories a,child_categories b,sub_child_categories c');
        $this->db->where("a.parent_cat_id = b.parent_cat_id"); 
        $this->db->where("b.child_cat_id = c.child_cat_id");
        $result = $this->db->get()->result_array();
    

    【讨论】:

      【解决方案2】:

      您必须使用加入查询,因为这是代码片段

          $this->db->select('a.parent_cat_id,a.parent_cat_name, b.child_cat_id,b.child_cat_name,c.sub_child_cat_id,c.sub_child_cat_name'); 
          $this->db->from('parent_categories a');
          $this->db->join('child_categories b', 'b.parent_cat_id = a.parent_cat_id', 'left'); 
          $this->db->join('sub_child_categories c', 'c.child_cat_id = b.child_cat_id', 'left'); 
          $query = $this->db->get();
          $res =  $query->result();
      

      【讨论】:

        【解决方案3】:

        我没有要检查的表结构和数据。但是,试试这个。它会起作用的。

         $this->db->select('a.parent_cat_id,a.parent_cat_name, b.child_cat_id,b.child_cat_name,c.sub_child_cat_id,c.sub_child_cat_name');
         $this->db->from('parent_categories a,child_categories b,sub_child_categories c');
         $this->db->join('a.parent_cat_id','b.parent_cat_id'); 
         $this->db->join('b.child_cat_id','c.child_cat_id');
         $this->db->get();
        

        【讨论】:

        • 我收到错误:表 'a.parent_cat_id' 不存在我已附加数据库结构
        • 可以导出sql给我吗?
        猜你喜欢
        • 2016-01-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-03-10
        • 2015-12-27
        • 1970-01-01
        • 2014-10-10
        相关资源
        最近更新 更多