Home  >  Article  >  Web Front-end  >  CI框架数据库查询之join用法分析_php实例

CI框架数据库查询之join用法分析_php实例

WBOY
WBOYOriginal
2016-06-07 17:07:05985browse

本文实例讲述了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

更多关于CodeIgniter相关内容感兴趣的读者可查看本站专题:《codeigniter入门教程》、《CI(CodeIgniter)框架进阶教程》、《php优秀开发框架总结》、《ThinkPHP入门教程》、《ThinkPHP常用方法总结》、《Zend FrameWork框架入门教程》、《php面向对象程序设计入门教程》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总》

希望本文所述对大家基于CodeIgniter框架的PHP程序设计有所帮助。

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn