php Codeigniter 中的多重连接
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4916498/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Multiple Joins in Codeigniter
提问by Seth
I'm new to building databases and I'm trying to do a JOIN based on a having three database tables.
我是构建数据库的新手,我正在尝试基于具有三个数据库表的 JOIN 进行连接。
Table A = ID, Name, etc
Table B = ID, Name, etc
Table C = ID, TableAId, TableBId
What I can't figure out is using active record how to make this selection. I'm trying to make as few requests as possible, but am getting stumped on how it should all be written without doing three separate calls.
我无法弄清楚的是使用活动记录如何进行此选择。我正在尝试尽可能少地提出请求,但是我对如何在不进行三个单独调用的情况下编写所有请求感到困惑。
回答by Michael Ozeryansky
$this->db->select('*');
$this->db->from('TableA AS A');// I use aliasing make joins easier
$this->db->join('TableC AS C', 'A.ID = C.TableAId', 'INNER');
$this->db->join('TableB AS B', 'B.ID = C.TableBId', 'INNER');
$result = $this->db->get();
The join function works like this: join('TableName', 'ON condition', 'Type of join');
join 函数的工作方式如下: join('TableName', 'ON condition', 'Type of join');
The equivilent sql:
等效的 sql:
SELECT *
FROM TableA AS A
INNER JOIN TableC AS C
ON C.TableAId = A.ID
INNER JOIN TableB AS B
ON B.ID = C.ID
I found that writing the SQL first, testing it, then converting to the active record style minimizes error.
我发现首先编写 SQL,测试它,然后转换为活动记录样式可以最大限度地减少错误。
回答by Ross
$this->db->select('*');
$this->db->from('blogs');
$this->db->join('comments', 'comments.id = blogs.id');
$this->db->join('authors', 'authors.id = comments.author_id');
hopefully you get my example.
希望你能得到我的例子。
Just add another $this->db->join()
;
只需添加另一个$this->db->join()
;
For complex queries you might be better off looking at an ORM such as doctrine
对于复杂的查询,您最好查看 ORM,例如学说
回答by Faisal
$this->db->select('*');
$this->db->from('table1');
$this->db->join('table2','table1.id=table2.id');
$this->db->join('table3','table2.id=table3.id');
$this->db->join('table4','table3.id=table4.id');
$this->db->join('table5','table5.id=table4.id');
$this->db->where('table5.id',$this->session->userdata('id'));//getting value from session and match the id of table5 and then show data
$data=$this->db->get()->result();//all data store in $data variable
回答by Aivan Monceller
if you want a flexible query you could use:
如果你想要一个灵活的查询,你可以使用:
http://codeigniter.com/user_guide/database/results.html
http://codeigniter.com/user_guide/database/results.html
which utilizes the following syntax $query = $this->db->query('SELECT * FROM my_table');
它使用以下语法 $query = $this->db->query('SELECT * FROM my_table');
here is the query:
这是查询:
SELECT a.name as namea ,b.name as nameb FROM tablec c
JOIN tablea a ON a.ID = c.ID
JOIN tableb b ON b.ID = c.ID
you may want to read more about joins here
您可能想在此处阅读有关联接的更多信息
then go through your results in such a way:
然后以这种方式查看您的结果:
$query = $this->db->query("YOUR QUERY");
$query = $this->db->query("YOUR QUERY");
foreach ($query->result_array() as $row)
{
echo $row['namea'];
echo $row['nameb'];
}