//
$this->db->select('a.*, b.name as b_name');
$this->db->from('table1 a');
$this->db->join('table2 b','a.cid = b.id','left');
$this->db->where('a.id', $id);
$res = $this->db->get()->result();
来自
http://zhidao.baidu.com/link?url=jhZqPIgp9gk4BQ8x032Ds82MRwaQF6vZWQoj0OafDP0KQwX1TXr76hxmciBYYrbtqG1...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();
|
|
|
|
| | |
| | |
在保证sql无误的情况下~~ 单对照着sql看了下 AR部分 试试 $this->db->join('db_category as s', 's.parent_id=c.cat_id','left'); |
|
| |
|
| | |
| | |
| 如果设置了表的前缀,就不能用别名了,否则会找不到表 |
|
| |
|
| | |
| | |