mysql - 父类别与所有子类别的数据 MYSQL 查询到 Codeigniter Active Record

标签 mysql codeigniter activerecord

大家好,因为这个查询我可以在 MYSQL 中工作,但我希望它可以在 Codeigniter 事件记录中转换。

基本上我有3张 table

  • 供应商(包含 intCat、intCity、*)
  • 类别(带有 intId、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))。

提前致谢。

最佳答案

这将通过父 ID 返回父类别及其类别和子类别

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

关于mysql - 父类别与所有子类别的数据 MYSQL 查询到 Codeigniter Active Record,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14621928/

相关文章:

大型表(> 8000 行)上的 MySQL MIN GROUP BY

php - mysql_close() 和 unset() 有什么区别?

jquery - Codeigniter 和 ajax 中的计时器

php - Codeigniter CSRF 仅对一次 ajax 请求有效

ruby-on-rails - Rails 3 has_many 改变了吗?

PHP/MySQL - 将数据库值转换为 MD5,然后缩小到该值的一部分?

mysql - 免费的BSD。 nginx、php-fpm、PDO_mysql 驱动程序不起作用

php - Braintree Paypal 无法在 Codeigniter 中工作

ruby-on-rails - 检查 JSONB 列中的数组是否包含另一个数组中的任何值

ruby - 奇怪的 NoMethodError(未定义方法 `name' 为 nil :NilClass)