简体   繁体   English

codeigniter join 2 表数据

[英]codeigniter join 2 table data

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.大家好,我是 codeigniter 的新手,目前正在项目中的一个小项目中工作,我正在尝试连接两个表并在单个表中显示那里的数据。 i looked at the user guide that codeigniter has an i am not sure how this work我查看了 codeigniter 的用户指南,我不确定这是如何工作的

$this->db->join();

what table should be first and what id key should be firs.哪个表应该是第一个,什么 id 键应该是第一个。 Can someone explain me more in detail about this please use examples if u can.如果可以的话,有人可以更详细地解释我吗,请使用示例。 I am trying to join credential table and tblanswers.我正在尝试加入凭证表和 tblanswers。 Tnx for answering. 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?编辑:是否可以使用简单的函数来分别检索两个表数据,而不是在 codeigniter 中使用 join 方法? 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 ?我想要的只是将数据库表中的数据回显到我网站页面中要显示的 html 表中是否可以编写两个 get 函数来分别检索两个表?

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();
?>

here is how it works:下面是它的工作原理:

  1. 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_id column注意:但请记住,如果您想使用 where 条件 / 此处我们假设两个表都有std_id列,则其中一列应该匹配

  1. 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_name form library table.*来自学生表和book_name表单库表。*

      $this->db->select('name, phone_number, book_name');
  1. Now write the from query and write one of the table name(No rule)现在编写 from 查询并编写表名之一(无规则)

     $this->db->from('student');
  2. Now join this with the another table with join query现在用连接查询将它与另一个表连接起来

     $this->db->join('library', 'students.std_id=library_std_id');
  3. 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)现在写出你想要书名形式库表 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.就是这样,现在您可以打印并检查结果了。

$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.您必须在表中插入一个名为 blogs_id(int value unisgned)的字段,因为博客可以有更多评论。 Isn't important the position of first or second value第一个或第二个值的位置不重要

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.您可以随心所欲地更改查询,执行跟踪和错误方法以获得适当的结果。

This is my code for joint many tables as possible.这是我尽可能连接多个表的代码。

I have 3 tables professeurs , publications and support .我有 3 桌professeurspublicationssupport

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;
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM