繁体   English   中英

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

[英]How to write this query in codeigniter active records

如何在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' 

在此处输入图片说明

尝试如下更改查询中的$this->db->where 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();

您必须为此使用联接查询

    $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();

我没有要检查的表结构和数据。 但是,尝试一下。 它会工作。

 $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();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM