php codeigniter join 2 表数据
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15200548/
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
codeigniter join 2 table data
提问by Edvinas Liutvaitis
hi everyone i am new to codeigniter and currently working on a small project in the project i am trying to join two tables and display there data in single table. i looked at the user guide that codeigniter has an i am not sure how this work
大家好,我是 codeigniter 的新手,目前正在项目中的一个小项目中工作,我正在尝试连接两个表并在单个表中显示那里的数据。我查看了 codeigniter 的用户指南,我不确定这是如何工作的
$this->db->join();
$this->db->join();
what table should be first and what id key should be firs. Can someone explain me more in detail about this please use examples if u can. I am trying to join credential table and tblanswers. Tnx for answering.
哪个表应该是第一个,什么 id 键应该是第一个。如果可以的话,有人可以更详细地解释我吗,请使用示例。我正在尝试加入凭证表和 tblanswers。Tnx 回答。
i have tried to code a function using this example:
我试图使用这个例子编写一个函数:
$this->db->select('*');
$this->db->from('blogs');
$this->db->join('comments', 'comments.id = blogs.id');
$query = $this->db->get();
EDIT:instead of using join method in codeigniter is it possible to use a simple function to retrieve the two table data separately? all i want is to echo the data from database table on to a html table in my website page to be displayed is it possible to write two get functions to retrieve two tables separately ?
编辑:是否可以使用简单的函数来分别检索两个表数据,而不是在 codeigniter 中使用 join 方法?我想要的只是将数据库表中的数据回显到我网站页面中要显示的 html 表中是否可以编写两个 get 函数来分别检索两个表?
回答by rafciokk0
It doesn't matter what table is first... Simply:
首先是什么表并不重要......简单地说:
<?php
$this->db->select('t1.name, t2.something, t3.another')
->from('table1 as t1')
->where('t1.id', $id)
->join('table2 as t2', 't1.id = t2.id', 'LEFT')
->join('table3 as t3', 't1.id = t3.id', 'LEFT')
->get();
?>
回答by Detroit Charan
here is how it works:
下面是它的工作原理:
- suppose we have two tables namely student, library.
- 假设我们有两个表,分别是学生、图书馆。
note: but remember that one of the column should match if you want to use where condition/ here we assume that both tables have std_idcolumn
注意:但请记住,如果您想使用 where 条件 / 此处我们假设两个表都有std_id列,则其中一列应该匹配
- Write the the select query as follows, in the brackets write what are all the things you want
- 如下编写select查询,在括号中写下你想要的所有东西
note:write as shown below don't put quotes to each single one put it on whole at once.
注意:按如下所示写,不要在每个单独的地方加上引号,一次把它放在一个整体上。
*note: suppose we want name, phone_no.from student table and book_nameform library table.*
*注意:假设我们想要姓名,电话号码。来自学生表和book_name表单库表。*
$this->db->select('name, phone_number, book_name');
Now write the from query and write one of the table name(No rule)
$this->db->from('student');Now join this with the another table with join query
$this->db->join('library', 'students.std_id=library_std_id');Now write the where condition that like you want book name form library table where std id=1(in practical you need to fetch this id from view/database)
$this->db->where('std_id', 1); $q= $this->db->get();
现在编写 from 查询并编写表名之一(无规则)
$this->db->from('student');现在用连接查询将它与另一个表连接起来
$this->db->join('library', 'students.std_id=library_std_id');现在写出你想要书名形式库表 where std id=1 的 where 条件(实际上你需要从视图/数据库中获取这个 id)
$this->db->where('std_id', 1); $q= $this->db->get();
That's it it's done now you can print and check the result.
就是这样,现在您可以打印并检查结果了。
回答by Alessandro Minoccheri
$this->db->join('comments', 'comments.id = blogs.id');
With this line you tell: search me inside comments all comments with id equal blogs.id.
用这一行告诉你:在评论中搜索我所有 id 等于 blogs.id 的评论。
Usually is something like that I think:
通常是这样的,我认为:
$this->db->join('comments', 'comments.blogs_id = blogs.id');
You have to insert into your table a field named blogs_id (int value unisgned) because blogs can have more comments. Isn't important the position of first or second value
您必须在表中插入一个名为 blogs_id(int value unisgned)的字段,因为博客可以有更多评论。第一个或第二个值的位置不重要
回答by Rajasekhar
Hi this will work for joining two tables in codeIgnator.
嗨,这将适用于在 codeIgnator 中连接两个表。
$this->db->select("chat.id,chat.name,chat.email,chat.phone,chat.date,post.post");
$this->db->from('chat');
$this->db->join('post', 'chat.id = post.id');
$query = $this->db->get();
if($query->num_rows() != 0)
{
return $query->result();
}
else
{
return false;
}
You can change to the query as you like do trail and error method to get your appropriate results.
您可以随心所欲地更改查询,执行跟踪和错误方法以获得适当的结果。
回答by Mbra Oliver
This is my code for joint many tables as possible.
这是我尽可能连接多个表的代码。
I have 3 tables professeurs,publicationsand support.
我有 3 张桌子professeurs,publications和support.
public function toutesdonnées(){
$this->db->select("*");
$this->db->from('publication');
$this->db->join('support', 'publication.idsup = support.idsup');
$this->db->join('professeurs', 'publication.emailprof = professeurs.emailprof');
$query = $this->db->get();
if($query->num_rows() != 0)
{
return $query->result();
}
else
{
return false;
}

