|
sql是这样的:
SELECT c.cat_id, c.cat_name, COUNT(s.cat_id) AS has_children FROM db_category AS c
LEFT JOIN db_category AS s ON s.parent_id=c.cat_id
GROUP BY c.cat_id ORDER BY c.parent_id, c.sort_order ASC
表结构是这样的
[td]
| 字段 | 类型 |
|
|
|
|
|
|
| cat_id | smallint(5) |
|
| cat_name | varchar(90) |
|
| keywords | varchar(255) |
|
| cat_desc | varchar(255) |
|
| parent_id | smallint(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();
|
|