0

大家好,作为这个查询,我可以在 MYSQL 中工作,但我希望它在 Codeigniter 活动记录中进行转换。

基本上我有3张桌子

  • 供应商(与 intCat、intCity、*)
  • 类别(带 in​​tId、strCategory、intParent)
  • tblcitylist(带有city_id,city_name)

询问:

SELECT category.strCategory AS strParent, Query3. *  FROM (

SELECT tblcitylist.city_name AS strCity, Query2. *  FROM (

SELECT Query1.intParent, Query1.strCategory, suppliers. *  FROM (

SELECT category.strCategory, category.intId, category.intParent FROM
category WHERE ( ( ( category.intParent ) =51 ) ) ) AS Query1 INNER
JOIN suppliers ON Query1.intId = suppliers.intCat ) AS Query2 INNER
JOIN tblcitylist ON Query2.intCity = tblcitylist.city_id ) AS Query3
INNER JOIN category ON Query3.intParent = category.intId

我想从父类别(51)的所有子类别中获取供应商(其类别和父类别名称(strCategory,strParent)和城市名称(city_name as strCity))。

提前致谢。

4

1 回答 1

1

这将通过传递 parent id 返回 parent ,他们的类别和子类别

function get_parent_child_subchild($category_id,$all_cate=array())
{
    if(!is_array($category_id))
    {           
        $this->db->where('parent_id', $category_id);
        $all_cate[]=$category_id;
    }
    else
    {           
        $this->db->where_in('parent_id', $category_id);
    }        
    $get_categories= $this->db->get('newcategory'); 
    if($get_categories->num_rows()>0)
    {
        $categories_vales=$get_categories->result(); 
        $new_subcat = array();
        foreach($categories_vales as $cate_val)
        {
            $category_id=$cate_val->category_id;
            array_push($new_subcat,$category_id);
        }
        $all_cate = array_merge($all_cate,$new_subcat);         
        if(count($new_subcat)>0)
        {
            $this->get_parent_child_subchild($new_subcat,$all_cate);
        }   
    }   

    return $all_cate;
}
于 2016-03-09T10:33:55.970 回答