The examples in this article describe the join usage in CI framework database query. Share it with everyone for your reference, the details are as follows:
Use each ID in table A to query the information of this ID in the people table. The sentence is as follows:
$this->db->from('A'); $this->db->join('B', 'sites.id = B.id');
Use each ID in table A to query the information of this ID in table B.
Pay attention to the SQL convention. If a column name is repeated in two tables, you need to add the table name and a "." sign before the column name. So sites.id in the location table means that the table where the id is located is sites. When performing SQL multi-table queries, it is best to uniquely identify column names. This can avoid ambiguity and make it clear to you.
For example: you execute the following statement
$this->db->select('*'); $this->db->from('blogs'); $this->db->join('comments', 'comments.id = blogs.id'); $query = $this->db->get();
Equivalent to executing this sql statement
SELECT * FROM blogs JOIN comments ON comments.id = blogs.id
If you want to use multiple connections in the query, you can call this function multiple times.
If you need to specify the type of JOIN, you can specify it through the third parameter of this function. Options include: left, right, outer, inner, left outer, and right outer.
$this->db->join('comments', 'comments.id = blogs.id', 'left'); // 生成: LEFT JOIN comments ON comments.id = blogs.id
Readers who are interested in more CodeIgniter related content can check out the special topics of this site: "codeigniter introductory tutorial", "CI (CodeIgniter) framework advanced tutorial", "php excellent development framework summary", "ThinkPHP introductory tutorial", "Summary of Common Methods in ThinkPHP", "Introduction Tutorial on Zend FrameWork Framework", "Introduction Tutorial on PHP Object-Oriented Programming", "Introduction Tutorial on PHP MySQL Database Operation" and "Summary of Common PHP Database Operation Skills"
I hope this article will be helpful to everyone’s PHP program design based on the CodeIgniter framework.