2
我试图从这个表中使用slu get获得结果的麻烦。mySQL在codeigniter上的分层查询
| id | parent | slug | name |
-----------------------------------------
| 1 | 0 | animations | animations |
| 2 | 1 | flash | flash |
| 3 | 2 | looped | looped |
| 4 | 1 | gif | gif images |
例如,我需要获取父类为“动画”并且子类为“flash”的类别。
真正的问题是因为我需要使用category/$ parent_slug/$ child_slug来代替使用ID(category/$ id)来搜索结果|3|2|looped|looped|
。
这是我到目前为止:
function get_category_childrens($category_parent=null){
$this->db->select('*');
if(!is_null($category_parent)){
$this->db->where('categories.slug', $category_parent);
$this->db->join('categories as l1', 'l1.parent = categories.id', 'left');
}
else{
$this->db->where('categories.parent', '0');
}
$query = $this->db->get('categories');
return $query->result_array();
}
生成的SQL:
SELECT *
FROM (`categories`)
LEFT JOIN `categories` as l1 ON `l1`.`parent` = `categories`.`id`
WHERE `categories`.`slug` = 'animations'
,如果你不知道CI,如果你的查询或它的一个想法,请没问题评论。
这不行,你搞砸了关于第二次连接xP – 2011-02-12 17:43:15
怎么这样?你说“父母是动画,孩子是闪光的”。 – 2011-02-12 18:21:35