好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

ci框架查询中join的使用

用 A表中的每个ID 去查询这个 ID 在 people 表中的信息。语句如下:


$this->db->from('A');
$this->db->join('B', 'sites.id = B.id');


用 A表中的每个ID 去查询这个 ID 在 B表中的信息。

注意SQL的约定,如果一个列名在二张表中是重复的,你需要在列名前加上表名和一个[."号。因此sites.id在位置桌子中意谓id所在的表是sites。在进行SQL多表查询时,最好把列名进行唯一性的标识,这样可以避免产生岐义,也可以让你自己明了。


如:你执行以下语句


 
	 
		 $this  ->  db  ->  select  (  '*'  );  
	 
		 $this  ->  db  ->  from  (  'blogs'  );  
	 
		 $this  ->  db  ->  join  (  'comments'  ,     'comments.id = blogs.id'  );  
	 
		    
	 
		 $query   =   $this  ->  db  ->  get  ();  
 

相当于 执行这条sql语句 SELECT * FROM blogs JOIN comments ON comments.id = blogs.id

如果你想要在查询中使用多个连接,可以多次调用本函数。

如果你需要指定 JOIN 的类型,你可以通过本函数的第三个参数来指定。可选项包括:left, right, outer, inner, left outer, 以及 right outer.

$this->db->join('comments', 'comments.id = blogs.id', 'left'); // 生成: LEFT JOIN comments ON comments.id = blogs.id

查看更多关于ci框架查询中join的使用的详细内容...

  阅读:73次