ci框架中join链接查询中怎么使用别名
sql是这样的:SELECT c.cat_id, c.cat_name, COUNT(s.cat_id) AS has_children FROM db_categoryAS c
LEFT JOIN db_category AS s ON s.parent_id=c.cat_id
GROUP BY c.cat_idORDER BY c.parent_id, c.sort_order ASC
表结构是这样的
字段类型
cat_idsmallint(5)
cat_namevarchar(90)
keywordsvarchar(255)
cat_descvarchar(255)
parent_idsmallint(5)
请问如果用ci的 join来写这个语句怎么写,下面是我用的方法,返回空,不知道哪里用得不对
$this->db->select('c.cat_id, c.cat_name,COUNT(s.cat_id) AS has_children');
$this->db->from('db_category as c');
$this->db->join('db_category as s', 's.parent_id=c.cat_id');
$this->db->group_by("c.cat_id");
$this->db->order_by("c.parent_id, c.sort_order", 'DESC');
$query = $this->db->get();
在保证sql无误的情况下~~
单对照着sql看了下 AR部分 试试$this->db->join('db_category as s', 's.parent_id=c.cat_id','left'); 如果设置了表的前缀,就不能用别名了,否则会找不到表 设置了,别名,如果字段名一样,还是会被覆盖的
页:
[1]