Mysql Codeigniter活动记录,其中JSON对象中的值

Mysql Codeigniter活动记录,其中JSON对象中的值,mysql,codeigniter,Mysql,Codeigniter,我有个问题要问。首先,我有一个表,其中父项有parent\u id是0,子项有parent\u id相等的父项id。所有子项的parent\u id存储为json编码的数组(一个子记录可以有多个父项) 所以,当我通过一个家长id时,我如何才能获得家长的所有孩子。我试过了,但不起作用,我也不知道 代码如下: function get_child_product($parent_id, $limit, $start) { $this -> db -> from('prod

我有个问题要问。首先,我有一个表,其中父项有
parent\u id
0
,子项有
parent\u id
相等的父项id。所有子项的
parent\u id
存储为json编码的数组(一个子记录可以有多个父项)

所以,当我通过一个家长id时,我如何才能获得家长的所有孩子。我试过了,但不起作用,我也不知道

代码如下:

function get_child_product($parent_id, $limit, $start) {
        $this -> db -> from('product');
        $this -> db -> where(json_decode('parent_id'), $parent_id);
        $this -> db -> limit($limit, $start);
        $this -> db -> order_by('order', 'asc');
        $this -> db -> order_by('id', 'desc');
        $query = $this -> db -> get();
        return $query -> result();
    }
解决的问题:

function get_child_product($parent_id, $limit, $start) {
        $this -> db -> from('product');
        $this -> db -> like('parent_id', '"' . $parent_id . '"');
        $this -> db -> limit($limit, $start);
        $this -> db -> order_by('order', 'asc');
        $this -> db -> order_by('id', 'desc');
        $query = $this -> db -> get();
        return $query -> result();
    }

你确定你不是故意的吗

where('parent_id', decoded($parent_id));

你确定你不是这个意思吗

where('parent_id', decoded($parent_id));

尝试
活动记录的
子句中的where_

function get_child_product($parent_id, $limit, $start) {
    $this -> db -> from('product');
    $this -> db -> where_in( parent_id, decoded($parent_id) ); #changes here
    $this -> db -> limit($limit, $start);
    $this -> db -> order_by('order', 'asc');
    $this -> db -> order_by('id', 'desc');
    $query = $this -> db -> get();
    return $query -> result();
}

尝试
活动记录的
子句中的
where_

function get_child_product($parent_id, $limit, $start) {
    $this -> db -> from('product');
    $this -> db -> where_in( parent_id, decoded($parent_id) ); #changes here
    $this -> db -> limit($limit, $start);
    $this -> db -> order_by('order', 'asc');
    $this -> db -> order_by('id', 'desc');
    $query = $this -> db -> get();
    return $query -> result();
}

如果我理解正确,您的数据库中有一个JSON编码的数组,它具有父关系,并且您只想获取某个父关系的子关系。问题是数据库中的JSON对象只不过是字符串,您不能在查询中动态解码它们并使用where子句

您有两个选择:

1.查询您的所有孩子,然后使用PHP根据解码的JSON对他们进行过滤

2.使用mysql
like
匹配json格式的字符串

function get_child_product($parent_id, $limit, $start) {
    return $this -> db -> from('product')
                    -> like('parent_id', '"parent_id":'.$parent_id)
                    -> limit($limit, $start)
                    -> order_by('order', 'asc')
                    -> order_by('id', 'desc')
                    -> get()
                    -> result();
}

请注意,
like
参数应该与JSON的语法相匹配,因此如果您使用的是
引号,然后将它们添加到参数中

如果我理解正确,您的数据库中有一个JSON编码的数组,具有父关系,并且您只想获取某个父关系的子项。问题是数据库中的JSON对象只不过是字符串,您不能在查询中动态解码它们并使用where子句

您有两个选择:

1.查询您的所有孩子,然后使用PHP根据解码的JSON对他们进行过滤

2.使用mysql
like
匹配json格式的字符串

function get_child_product($parent_id, $limit, $start) {
    return $this -> db -> from('product')
                    -> like('parent_id', '"parent_id":'.$parent_id)
                    -> limit($limit, $start)
                    -> order_by('order', 'asc')
                    -> order_by('id', 'desc')
                    -> get()
                    -> result();
}

请注意,
like
参数应该与JSON的语法相匹配,因此如果您使用的是
引号,然后将它们添加到参数中

您能说得更清楚一点吗?JSON在数据库中吗?还是向函数发送一个id数组来获取这些父函数的所有子函数?是的,我在数据库中将父函数id数组存储为JSON。您能说得更清楚一点吗?JSON在数据库中吗?或者您正在向函数发送id数组以获取这些父对象的所有子对象?是的,我在数据库中将父对象id数组存储为JSON。在
'parent\u id'
周围缺少引号。在
'parent\u id'
周围缺少引号。